pythondjangopython-2.7django-1.6

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.


Solution

  • 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 $.