2017-09-12 70 views
0

我已經在我的項目模板下面的鏈接:Django的NoReverseMatch,不是註冊名稱空間

<li><a id="toggleLogin" href= "{% url 'login' %}" onclick="toggleLogin();" ><span>Login</span></a></li> <!-- login app 
--> 

項目網址是:

url(r'^login/',loginViews.user_login,name='login'), 

應用網址是:

url(r'^$',views.user_login,name='user_login'), 

and the ap摺疊觀點是:

def user_login(request): 
    """User at login view """ 
    # 
    if request.method == 'POST': 
     # First get the username and password supplied 
     username = request.POST.get('username') 
     password = request.POST.get('password') 
     # Django's built-in authentication function: 
     user = authenticate(username=username, password=password) 
     # If we have a user 
     if user: 
      #Check it the account is active 
      if user.is_active: 
       # Log the user in. 
       login(request,user) 
       # Send the user back to some page. 
       # In this case their homepage. 
       return HttpResponseRedirect(reverse('index')) 
      else: 
       # If account is not active: 
       return HttpResponse("Your account is not active.") 
     else: 
      print("Someone tried to login and failed.") 
      print("They used username: {} and password: {}".format(username,password)) 
      return HttpResponse("Invalid login details supplied.") 
    else: 
     #Nothing has been provided for username or password. 
     return render(request, 'login.html', {}) 

但一旦鏈接的點擊,則返回Django的以下錯誤:

NoReverseMatch at /login/ 
'login_app' is not a registered namespace 

應此命名空間中的項目的URL文件進行註冊呢?

回答

0

對! 所以正確的方法是:

HTML

<li><a id="toggleLogin" href= "{% url 'login_app:user_login' %}" onclick="toggleLogin();" 

項目網址

url(r'^login/',include('login_app.urls',namespace='login_app')), 

和應用網址

url(r'^$',views.user_login,name='user_login'), 

雖然它導致了另一個關於執行嵌入式javascript函數的問題。但我會爲此創建一個新問題。 謝謝。

1

根本沒有使用「應用程序url」。您已經在項目URL中完全定義了URL(如login),因此不會創建namespace。通常情況下,你會使用include鏈接到應用程序的URL,但你沒有這樣做。

相關問題