NoReverseMatch Error

强颜欢笑 提交于 2019-11-27 02:30:27

问题


I keep getting this error for the django login system. Here is part of my urls.py:

     (r'^contractManagement/login', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),

The exact error I am getting:

Exception Type: NoReverseMatch
Exception Value:    Reverse for ''django.contrib.auth.views.login'' with arguments '()' and keyword arguments '{}' not found.

I can't understand why i am getting this error. If you need anything else let me know.


回答1:


You don't show where you are trying to reverse this URL, but it looks like you have double-quoted it. If you're using the url tag, note that you don't need quotes around the url name:

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

not

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



回答2:


You see that ''the.unknown.view'' is reported including too many qoutes.

It is because the quoted syntax will be valid in Django 1.5 and higher. For Django 1.3 or 1.4, you should activate the future behavior by this line in the template:

{% load url from future %}

which is valid also for Django 1.5.


Example for Django 1.5+

{% url "path.to.some.view" %}

Classic syntax for Django <= 1.4.x (without "future" command) is:

{% url path.to.some.view %}



回答3:


I would give your url a name (in order to do that, you need to use the url method) Also you should add a trailing slash to all your urls, cause the django CommonMiddleware is going to be doing a 302 redirect on all your urls if you don't:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
     url(r'^contractManagement/login/', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name='contract_login'),

)

Then you can use reverse in your code, or url in your templates, and if you ever decide to change the actual url (ie: changedCotractManagement/login/), as long as the name is the same, your code will still be good.

in code:

from django.core.urlresolvers import reverse
reverse('contract_login')

in template:

{% url contract_login %}

Edit: per MrOodles



来源:https://stackoverflow.com/questions/4981026/noreversematch-error

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