【问题标题】:Django Urls - Template Issue {% url '' %}Django Urls - 模板问题 {% url '' %}
【发布时间】:2020-02-22 04:06:13
【问题描述】:

当我开始使用 Django 时,我使用的是 1.11,然后升级到了 2.0。我的 URL 工作正常,没有问题,我想有些东西已经更新了,但我一直在查看文档,我看不出问题出在哪里。从我看到我在模板中的 URL 是正确的,即使我也尝试使用 '' 并且仍然遇到同样的问题。

NoReverseMatch at /admin/dashboard
Reverse for '' not found. '' is not a valid view function or pattern name.
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/dashboard
Django Version: 2.2.6
Exception Type: NoReverseMatch
Exception Value:    
Reverse for '' not found. '' is not a valid view function or pattern name.
Exception Location: /home/bridgetsarah/.local/lib/python3.5/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 673
Python Executable:  /usr/bin/python3
Python Version: 3.5.2
Python Path:    
['/home/bridgetsarah/voyage/bridgetsarah',
 '/usr/lib/python35.zip',
 '/usr/lib/python3.5',
 '/usr/lib/python3.5/plat-x86_64-linux-gnu',
 '/usr/lib/python3.5/lib-dynload',
 '/home/bridgetsarah/.local/lib/python3.5/site-packages',
 '/usr/local/lib/python3.5/dist-packages',
 '/usr/lib/python3/dist-packages']
Server time:    Fri, 25 Oct 2019 19:26:43 +0000

      <ul>
24      <li> <i class="fas fa-tachometer-alt"></i>  <a href="#"> Dashboard </a> </li>
25      <li><i class="fas fa-tasks"></i>  <a href='#'> Projects </a> </li>
26      <li><i class="fas fa-users"></i> <a href="{% url client %}"> Clients </a> </li>
27      <li><i class="fas fa-server"></i><a href='#'> Servers </a> </li>
28    </ul>  
29  
from django.urls import include, path , re_path
from django.conf.urls import  include, url
from . import views
from django.contrib.auth.views import LoginView
from voyage.views import login_invalid, dashboard, client


app_name = 'voyage'

urlpatterns = [
   
    
    # Clients---------------------------------- #
    path('admin/client', views.client, name='client'),]
def client(request):
    return render(request, 'admin/client/client.html', context)

接下来我可以尝试什么?

【问题讨论】:

    标签: python html django python-3.x django-urls


    【解决方案1】:

    应该是{% url "client" %} - 名称必须用引号引起来。

    注意,这在 Django 1.5(2012 年)中一直发生变化;这在 1.11 版中肯定行不通。

    【讨论】:

    • 感谢您的回答,但即使我也尝试这样做,问题仍然存在:(
    • @bridgetsarah 如果您按照 Daniel 的建议进行更改,您将收到 different 错误消息,例如Reverse for 'client' not found. 你可以通过包含命名空间来解决这个问题。 urls.py 中有 app_name = 'voyage',所以应该是 {% url "voyage:client" %}
    • 谢谢你!它确实有效,抱歉我之前没有尝试过使用 app_name 部分,我会对此进行修改:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    • 2017-11-05
    • 2010-11-16
    • 2012-01-26
    相关资源
    最近更新 更多