渲染时 NoReverseMatch:反转“django.contrib.auth.views.login”
我正在使用 Django 的身份验证,并且在 login.html 模板中,以下语句生成错误:
{% url 'django.contrib.auth.views.login' %}
/登录时出现模板语法错误
渲染时捕获 NoReverseMatch:未找到参数“()”和关键字参数“{}”的“django.contrib.auth.views.login”的反向。
这个网址在我的 urls.py 中定义:
(r'^login$', 'django.contrib.auth.views.login')
我已经安装了身份验证系统:
INSTALLED_APPS = (
'django.contrib.auth',
...
)
有什么想法吗?
I'm using Django's authentication, and in the login.html template, the following statement is generating an error:
{% url 'django.contrib.auth.views.login' %}
TemplateSyntaxError at /login
Caught NoReverseMatch while rendering: Reverse for ''django.contrib.auth.views.login'' with arguments '()' and keyword arguments '{}' not found.
This url is defined in my urls.py:
(r'^login
I have installed the auth system:
INSTALLED_APPS = (
'django.contrib.auth',
...
)
Any ideas?
, 'django.contrib.auth.views.login')
I have installed the auth system:
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 Django 1.10 开始:
从 Django 1.10 开始,不再可能在
url()
中使用字符串'django.contrib.auth.views.login'
或{% url %}
标签。首先,更改 url 模式以使用可调用,并命名 url 模式。例如:
然后更新您的 url 标记以使用相同的名称:
从 Django 1.5 开始:
您不再需要
{% load url from future %}
,只需使用引用的语法 ({% url 'django.contrib.auth.views.login' %}
) 就完成了(请参阅 Django 1.5 发行说明)。从 Django 1.3 开始:
请注意 Django 1.3(正如 Karen Tracey 下面指出的那样),解决此问题的正确方法是添加
:模板的顶部,然后使用:
在 Django 1.3 之前:
根据该错误消息判断(注意视图路径周围的双单引号),我猜
{% url ... %}
标签不想要报价,请尝试:As of Django 1.10:
As of Django 1.10, it is no longer possible to use the string
'django.contrib.auth.views.login'
inurl()
or the{% url %}
tag.First, change your url patterns to use the callable, and name the url pattern. For example:
Then update your url tag to use the same name:
As of Django 1.5:
You don't need
{% load url from future %}
any more, just use the quoted syntax ({% url 'django.contrib.auth.views.login' %}
) and you're done (see the Django 1.5 release notes).As of Django 1.3:
Note that as of Django 1.3 (as Karen Tracey points out below), the correct way to fix this is to add:
at the top of your template, and then use:
Prior to Django 1.3:
Judging by that error message (note the double single-quotes around the path to the view), I'd guess that the
{% url ... %}
tag doesn't want quotes, try:带引号的语法是 Django 1.3 中的新语法。修复 1.3 版转发错误的正确方法是在模板中包含 {% load url from future %}。
The syntax with quotes is new in Django 1.3. The correct way to fix the error on 1.3 forward would be to incldue {% load url from future %} in the template.