34

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.

sleblanc
  • 3,821
  • 1
  • 34
  • 42
Dean
  • 8,668
  • 17
  • 57
  • 86

3 Answers3

44

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' %}
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 6
    Is this a fault in the tutorial then? As i had just worked it out to be that. – Dean Feb 12 '11 at 22:52
  • 1
    Yes, this appears to be a flaw in the tutorial in the example at https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.views.login – Mark Chackerian Jul 21 '12 at 16:19
  • 6
    To summarise, Django 1.4 should not have quotes while 1.5 will need them. More details on the problem in this other question: http://stackoverflow.com/questions/10358929/url-gives-me-noreversematch-error-while-reverse-returns-the-url-just-fin – Bruno Girin Aug 10 '12 at 10:01
  • 1
    From Django 1.5 and onwards, use of qoutes is a part of syntax. For 1.4 and below no qoutes are required. – Nabeel Ahmed Apr 28 '15 at 11:06
19

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 %}
hynekcer
  • 14,942
  • 6
  • 61
  • 99
  • Ah, the example template in the [`views.login` docs](https://docs.djangoproject.com/en/1.4/topics/auth/#django.contrib.auth.views.login) contains the future url thing, but doesn't mention what it means (I assumed it was something to do with the `extends "base.html"` line, so ignored it, and.. thus ended up on this question!) – dbr Apr 21 '12 at 13:32
6

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

MattoTodd
  • 14,467
  • 16
  • 59
  • 76