2014-01-20 11 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') 

これをアクションとして持つフォーム:

唯一

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

しかし、私は

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

にURLパターンを設定した場合、それは正常に動作し、それは:

そして問題は、私はページをロードしようとすると、私が得る、ここでそれはですアクションを '/'に設定します。

私は答えを探していましたが、なぜ動作しないのか分かりません。

ログインURLパターンをurl(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

問題は、認証URLをメインのURLに含める方法にあります。 ^と$の両方を使用するため、空の文字列だけが一致します。 $を落とす。

+6

それは簡単ではないと信じられない、私は私の髪を引っ張っていた。ありがとうございました! – freakrho

+1

ありがとう、この回答は私に少なくとも1時間の絶望を免れました:) – maddob

+0

彼らはこれを修正するか、より良いエラーメッセージを作成する必要があります。私はWTFが間違っていることを理解しようと20分を費やしました。 – DataMania

関連する問題