2014-01-20 62 views
28

我在django 1.6(和python 2.7)中做了一个简单的登录应用程序,并且在开始时出现错误,导致我不能继续。Django NoReverseMatch

这是该网站的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)), 
) 

这是登录/ 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'), 
) 

这是登录/人次,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') 

我有以此为动作的表格:

{% url 'login:auth' %} 

而这正是问题是,当我尝试加载页面时,我得到:

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

但如果我URL模式设置为

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

它只是正常工作,它将操作设置为'/'。

我一直在寻找答案,我不明白为什么它不起作用。 (r'^ login/$',include('login.urls',namespace ='login')),它没有改变任何东西。

+0

可能重复[什么是NoReverseMatch错误,以及如何解决它?](http://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do- i-fix-it) – Sayse

回答

41

问题在于你在auth URL中包含主要URL的方式。 因为您同时使用^和$,只有空字符串匹配。放下$。

+6

我简直不敢相信那是那么简单,我把头发拉出来。谢谢! – freakrho

+1

谢谢,这个答案让我至少有一个小时的绝望:) – maddob

+0

他们应该修复这个问题或者创建一个更好的错误信息。我花了20分钟试图弄清楚WTF出错了。 – DataMania