Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django NoReverseMatch

I'm making a simple login app in django 1.6 (and python 2.7) and I get an error at the beggining that is not letting me continue.

This is the site's url.py

from django.conf.urls import patterns, include, url from django.contrib import admin import login  admin.autodiscover()  urlpatterns = patterns('',     url(r'^$', include('login.urls', namespace='login')),     url(r'^admin/', include(admin.site.urls)), ) 

And this is login/urls.py:

from django.conf.urls import patterns, url from login import views  urlpatterns = patterns('',     url(r'^$', views.index, name='index'),     url(r'^auth/', views.auth, name='auth'), ) 

This is login/views,py

from django.shortcuts import render from django.contrib.auth import authenticate  def auth(request):     user = authenticate(username=request.POST['username'], password=request.POST['password'])     if user is not None:         # the password verified for the user         if user.is_active:             msg = "User is valid, active and authenticated"         else:             msg = "The password is valid, but the account has been disabled!"     else:         # the authentication system was unable to verify the username and password         msg = "The username and password were incorrect."     return render(request, 'login/authenticate.html', {'MESSAGE': msg})  def index(request):     return render(request, 'login/login_form.html') 

I have a form that has this as action:

{% url 'login:auth' %} 

And that's where the problem is, when I try to load the page, I get:

Reverse for 'auth' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'$auth/'] 

But if I set the url pattern to

url(r'', views.auth, name='auth') 

it works fine, only it sets the action as '/'.

I've been looking all around for an answer and I don't understand why it doesn't work.

I tried changing the login url pattern to url(r'^login/$', include('login.urls', namespace='login')), and it didn't change anything.

like image 371
freakrho Avatar asked Jan 20 '14 17:01

freakrho


People also ask

What does NoReverseMatch mean in Django?

The NoReverseMatch error is saying that Django cannot find a matching url pattern for the url you've provided in any of your installed app's urls. The NoReverseMatch exception is raised by django. core. urlresolvers when a matching URL in your URLconf cannot be identified based on the parameters supplied.

How do I reverse in Django?

reverse() If you need to use something similar to the url template tag in your code, Django provides the following function: reverse (viewname, urlconf=None, args=None, kwargs=None, current_app=None)


1 Answers

The problem is in the way you include the auth URLs in the main one. Because you use both ^ and $, only the empty string matches. Drop the $.

like image 136
Daniel Roseman Avatar answered Oct 06 '22 01:10

Daniel Roseman