【问题标题】:URL won't redirect to detailview pk DjangoURL 不会重定向到 detailview pk Django
【发布时间】:2019-10-21 17:55:12
【问题描述】:

我有一个简单的项目,在修复重定向时遇到了麻烦。 该 url 没有重定向到我的 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 %}

正如您在下面的屏幕截图中看到的那样。如果我点击应该重定向到 statistics_detail.html 的clients.status,什么也不会发生

浏览器网址:http://127.0.0.1:8000/prov/blist/ 点击状态后只会添加http://127.0.0.1:8000/prov/blist/2146/但不起作用

【问题讨论】:

  • 仍然没有工作@BearBrown

标签: django python-3.x detailview


【解决方案1】:
<a href="{% url 'detail' clients.id %}">

【讨论】:

  • django.urls.exceptions.NoReverseMatch:找不到“详细信息”的反向。 'detail' 不是有效的视图函数或模式名称。更改为 {% url %} 后出现这些错误
  • 这似乎不是 urls.py 的完整内容 此外,如果这是应用程序的 urls.py 文件,请显示这些 urlpatterns 是如何包含在主 urlspatterns 中的。跨度>
【解决方案2】:

你需要一个前导斜杠:&lt;a href="/{{clients.id}}/"&gt;

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

【讨论】:

猜你喜欢
  • 2018-05-18
  • 1970-01-01
  • 1970-01-01
  • 2022-11-06
  • 2012-04-09
  • 2014-07-12
  • 2020-02-16
  • 2017-05-30
  • 2020-06-22
相关资源
最近更新 更多