渲染时 NoReverseMatch:反转“django.contrib.auth.views.login”

发布于 2024-10-10 04:25:55 字数 484 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里人 2024-10-17 04:25:55

从 Django 1.10 开始:

从 Django 1.10 开始,不再可能在 url() 中使用字符串 'django.contrib.auth.views.login'{% url %} 标签。

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

from django.contrib.auth import views as auth_views

url_patterns = [
    url(r'^login

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

{% url 'login' %}

从 Django 1.5 开始:

您不再需要 {% load url from future %} ,只需使用引用的语法 ({% url 'django.contrib.auth.views.login' %}) 就完成了(请参阅 Django 1.5 发行说明)。

从 Django 1.3 开始:

请注意 Django 1.3(正如 Karen Tracey 下面指出的那样),解决此问题的正确方法是添加

{% load url from future %}

:模板的顶部,然后使用:

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

在 Django 1.3 之前:

根据该错误消息判断(注意视图路径周围的双单引号),我猜 {% url ... %} 标签不想要报价,请尝试:

{% url django.contrib.auth.views.login %}
, auth_views.login, name='login'), ]

然后更新您的 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' in url() or the {% url %} tag.

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

Then update your url tag to use the same name:

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

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:

{% 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 django.contrib.auth.views.login %}
, auth_views.login, name='login'), ]

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:

等数载,海棠开 2024-10-17 04:25:55

带引号的语法是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文