NoReverseMatch while rendering:Reverse for''django.contrib.auth.views.login'' [英] NoReverseMatch while rendering: Reverse for ''django.contrib.auth.views.login''
问题描述
我使用Django的身份验证,在login.html模板中,以下语句产生错误:
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
TemplateSyntaxError at /login
在渲染时捕获NoReverseMatch:使用参数'()'和关键字参数''django.contrib.auth.views.login' '{}'未找到。
Caught NoReverseMatch while rendering: Reverse for ''django.contrib.auth.views.login'' with arguments '()' and keyword arguments '{}' not found.
此url在我的urls.py中定义:
This url is defined in my urls.py:
(r'^login$', 'django.contrib.auth.views.login')
我已经安装了auth系统:
I have installed the auth system:
INSTALLED_APPS = (
'django.contrib.auth',
...
)
任何想法?
推荐答案
截至Django 1.10:
Django 1.10,不可能使用字符串'django.c ontrib.auth.views.login'
in url()
或 {%url%}
标签。
首先,更改您的url模式以使用可调用,并命名url模式。例如:
First, change your url patterns to use the callable, and name the url pattern. For example:
from django.contrib.auth import views as auth_views
url_patterns = [
url(r'^login$', auth_views.login, name='login'),
]
然后更新您的URL标签以使用相同的名称:
Then update your url tag to use the same name:
{% url 'login' %}
从Django 1.5开始:
只需使用引用的语法( {%url'django.contrib.auth.views),不需要
),你完成了(参见 Django 1.5发行说明)。 {%从未来%}加载url
.login'%}
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).
请注意, Django 1.3的href =https://docs.djangoproject.com/en/dev/releases/1.3/#changes-to-url-and-ssi =nofollow noreferrer(as Karen Tracey指出以下),正确要解决这个问题的方法是添加:
Note that as of Django 1.3 (as Karen Tracey points out below), the correct way to fix this is to add:
{% load url from future %}
在模板的顶部,然后使用:
at the top of your template, and then use:
{% url 'django.contrib.auth.views.login' %}
Prior到Django 1.3:
根据该错误消息(注意视图的路径旁边的双引号),我猜想 {%url ...%}
标签不想要引号,请尝试:
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:
{% url django.contrib.auth.views.login %}
这篇关于NoReverseMatch while rendering:Reverse for''django.contrib.auth.views.login''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!