网址不会重定向到detailview pk Django

时间:2019-06-06 08:33:53

标签: django python-3.x detailview

我有一个简单的项目,在解决重定向问题上遇到了麻烦。 网址未重定向到我的statistics_detail.html。每当我单击它时,它只会在链接上添加pk,但不会重定向

这是我的 url.py

urlpatterns = [
    url('^$', views.index, name='index'),
    re_path(r'^(?P<pk>\d+)/$', views.StatisticsDetailView.as_view(), name='detail'),
    url('blist/', views.StatisticListView.as_view(), name='blist'),
    url('user_list', DisplayView.as_view(), name='user_list'),
    url('new_user', views.new_user, name='new_user'),
    url('push_user_tb', views.push_user_tb, name='push_user_tb'),
    url('push_user_prod', views.push_user_prod, name='push_user_prod'),
    url('st', views.display_stats_logs, name='st'),
    url('today', views.display_today, name='today'),
    url('balance', views.display_balance, name='balance'),
]

views.py

class StatisticsDetailView(DetailView):
    context_object_name = 'statistics_details'
    model = models.Statistics
    template_name = 'provision/statistics_detail

这里也是 statistics_detail.html

{% extends 'base.html' %}

{% block content %}
    <p>Statistics</p>

    <div class="container">
        <table class="table">
            <tr>
                <th>Name</th>
                <th>Mac ID</th>
                <th>Hours</th>
                <th>Date</th>
                <th>Status</th>
            </tr>
        {% for clients in object_list %}

            <tr>
                <td>{{ clients.name }}</td>
                <td>{{ clients.mac_add }}</td>
                <td>{{ clients.minutes_used|cut:".0" }}</td>
                <td>{{ clients.date }}</td>
                <td><a href="{{clients.id}}/">{{ clients.status }}</a></td>
            </tr>
        {% endfor %}
        </table>
        It is {% now "jS F Y H:i" %}
    </div>
{% endblock %}

如下面的屏幕截图所示。如果我单击clients.status(应该重定向到statistics_detail.html

),则没有任何反应

浏览器URL:http://127.0.0.1:8000/prov/blist/ 单击状态后,它将仅添加http://127.0.0.1:8000/prov/blist/2146/,但不起作用

enter image description here

2 个答案:

答案 0 :(得分:4)

<a href="{% url 'detail' clients.id %}">

答案 1 :(得分:3)

您需要一个斜杠:<a href="/{{clients.id}}/">

更好的是,使用{% url %}标签而不是手动输出URL。

相关问题