Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

python - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...