NoReverseMatch Error

前端 未结 3 971
感动是毒
感动是毒 2020-12-09 15:09

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\'         


        
相关标签:
3条回答
  • 2020-12-09 15:32

    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' %}
    
    0 讨论(0)
  • 2020-12-09 15:48

    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 %}
    
    0 讨论(0)
  • 2020-12-09 15:52

    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

    0 讨论(0)
提交回复
热议问题