渲染时的NoReverseMatch:反向为“django.致歉.auth.view .login”

时间:2022-09-04 12:32:44

I'm using Django's authentication, and in the login.html template, the following statement is generating an error:

我正在使用Django的身份验证和登录。html模板,下面的语句正在生成一个错误:

{% url 'django.contrib.auth.views.login' %}

TemplateSyntaxError at /login

TemplateSyntaxError在/登录

Caught NoReverseMatch while rendering: Reverse for ''django.contrib.auth.views.login'' with arguments '()' and keyword arguments '{}' not found.

在渲染时捕捉到NoReverseMatch:倒转到“django.。auth.views”。登录“与参数'()'和关键字参数'{}'没有找到。

This url is defined in my urls.py:

这个url在我的url中定义:

(r'^login$', 'django.contrib.auth.views.login')

I have installed the auth system:

我安装了auth系统:

INSTALLED_APPS = (
    'django.contrib.auth',
...
)

Any ideas?

什么好主意吗?

2 个解决方案

#1


64  

As of Django 1.10:

As of Django 1.10, it is no longer possible to use the string 'django.contrib.auth.views.login' in url() or the {% url %} tag.

在Django 1.10中,不再可能使用string ' django.. auth.views。在url()或{% url %}标签中登录'。

First, change your url patterns to use the callable, and name the url pattern. For example:

首先,更改url模式以使用可调用,并命名url模式。例如:

from django.contrib.auth import views as auth_views

url_patterns = [
    url(r'^login$', auth_views.login, name='login'),
]

Then update your url tag to use the same name:

然后更新您的url标记以使用相同的名称:

{% 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).

您不再需要来自future %}的{% load url,只需使用所引用的语法({% url ' django.致歉.views)。登录' %}),您就完成了(参见Django 1.5发布说明)。

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:

注意,对于Django 1.3(正如Karen Tracey在下面指出的),正确的解决方法是:

{% load url from future %}

at the top of your template, and then use:

在模板顶部,然后使用:

{% url 'django.contrib.auth.views.login' %}

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…标签不需要引号,试试:

{% url django.contrib.auth.views.login %}

#2


9  

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.

在Django 1.3中,带引号的语法是新的。修正1.3 forward错误的正确方法是在模板中添加来自未来%}的{% load url。

#1


64  

As of Django 1.10:

As of Django 1.10, it is no longer possible to use the string 'django.contrib.auth.views.login' in url() or the {% url %} tag.

在Django 1.10中,不再可能使用string ' django.. auth.views。在url()或{% url %}标签中登录'。

First, change your url patterns to use the callable, and name the url pattern. For example:

首先,更改url模式以使用可调用,并命名url模式。例如:

from django.contrib.auth import views as auth_views

url_patterns = [
    url(r'^login$', auth_views.login, name='login'),
]

Then update your url tag to use the same name:

然后更新您的url标记以使用相同的名称:

{% 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).

您不再需要来自future %}的{% load url,只需使用所引用的语法({% url ' django.致歉.views)。登录' %}),您就完成了(参见Django 1.5发布说明)。

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:

注意,对于Django 1.3(正如Karen Tracey在下面指出的),正确的解决方法是:

{% load url from future %}

at the top of your template, and then use:

在模板顶部,然后使用:

{% url 'django.contrib.auth.views.login' %}

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…标签不需要引号,试试:

{% url django.contrib.auth.views.login %}

#2


9  

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.

在Django 1.3中,带引号的语法是新的。修正1.3 forward错误的正确方法是在模板中添加来自未来%}的{% load url。