NoReverseMatch while rendering: Reverse for ''django.contrib.auth.views.login''

佐手、 提交于 2019-11-28 22:23:27

问题


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$', 'django.contrib.auth.views.login')

I have installed the auth system:

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

Any ideas?


回答1:


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$', auth_views.login, name='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 %}



回答2:


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.



来源:https://stackoverflow.com/questions/4578685/noreversematch-while-rendering-reverse-for-django-contrib-auth-views-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!