2015-06-13 84 views
4

背景

我想自定義一個Django項目認證的看法,但我似乎無法得到自定義password_change視圖來運行。我使用Django 1.8.2和Python 2.7。反向的「password_change_done」與參數「()」和關鍵字參數「{}」未找到

我的模塊userauthurls.py如下所示:

from django.conf.urls import patterns, include, url 

urlpatterns = patterns('django.contrib.auth.views', 
    url(r'^login/$', 'login', {'template_name': 'userauth/login.html'}, 
     name='userauth_login'), 
    url(r'^logout/$', 'logout', {'next_page': '/'}, 
     name='userauth_logout'), 
    url(r'^password-change/$', 'password_change', 
     {'template_name': 'userauth/password_change_form.html'}, 
     name='userauth_password_change'), 
    url(r'^password-change-done/$', 'password_change_done', 
     {'template_name': 'userauth/password_change_done.html'}, 
     name='userauth_password_change_done'), 
) 

這主要urls.py被引用爲這樣的:

urlpatterns = [ 
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^account/', include('userauth.urls')), 
] 

userauth/password_change_form.html

{% extends "base.html" %} 

{% block title %}{{ block.super }} - Change Password{% endblock %} 

{% block toggle_login %}{% endblock %} 

{% block content %} 
<form action="{% url 'userauth_password_change' %}" method="post" accept-charset="utf-8"> 
    {{ form.as_p }} 
    {% csrf_token %} 
    <input type="submit" value="Change password"/> 
</form> 
{% endblock %} 
模板

和模板fo [R userauth/password_change_done.html

{% extends "base.html" %} 

{% block title %}{{ block.super }} - Password change successful{% endblock %} 

{% block content %} 
<p>Your password has been changed successfully.</p> 
<a href="{% url 'products_product_index' %}">Back to your Account</a> 
{% endblock %} 

的問題

當我打開'password_change_done'頁(在/帳號/密碼轉換完成)​​,然後一切都很好。

但在'password-change'(/ accunt /密碼更改)我得到這個錯誤:

NoReverseMatch at /account/password-change/

Reverse for 'password_change_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

我試過

我不知道,爲什麼這應該發生。

  1. 我試圖從url 'userauth_password_change'
  2. 去除單引號我確信在urls.py存在password-change-done頁面,並提供
  3. 我在Reverse for '*' with arguments '()' and keyword arguments '{}' not foundDjango: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not foundDjango change_password NoReverseMatch at /accounts/password/change/閱讀解決方案(多了幾個,我嘗試了所有的解決方案,但我無法在自己的代碼中找到問題)

任何幫助表示讚賞。謝謝!

+0

當我將'name ='userauth_password_change_done''的名稱更改爲'name ='password_change_done''時,它可以工作。這是爲什麼?有人可以解釋嗎? – Rias

回答

3

的解決方案是,將在urls.py的password_change_done鏈接的名字必須'password_change_done'

url(r'^password-change-done/$', 'password_change_done', 
    {'template_name': 'userauth/password_change_done.html'}, 
    name='password_change_done'), 

我進去一看django.contrib.auth.views.password_change(這是產生問題),並意識到,日e url 'password_change_done'在Django 1.8.2中有硬編碼。

3

在某些部分,你調用一個名爲「password_change_done」

正確的名稱是url:「userauth_password_change_done」

+0

那是哪裏?我找不到錯誤地使用它。它應該是'name ='password_change_done''而不是? – Rias

+0

你可以添加名爲「password_change」的視圖嗎? – alexander

+0

其實我沒有'password_change'的自定義視圖。它雖然只是使用''django.contrib.auth.views-password_change'',因爲lin'parapatterns = patterns('django.contrib.auth.views',.我是否明白錯誤? – Rias

1

您需要周圍的視圖名稱刪除單引號

{%URL password_change_done%}的 代替

{%URL 'password_change_done' %}

4

好了,所以我建議的解決方案在這裏沒有工作。我在具有特定應用程序標籤的應用程序中使用Django 1.8.8,所以我需要在像這樣的模板中指定url,例如app_label:URL_NAME。這意味着password_change_done的反向將無法工作,因爲它是app_label:password_change_done。

但幸好有一個解決方案:'post_change_redirect'。因此,我指定password_change這樣的:

url(r'^password_change$', 'django.contrib.auth.views.password_change', {'template_name': 'password_change.html', 'post_change_redirect': 'app_label:password_change_done'}, name='password_change'), 

我相信其他人可以使用它來克服上述問題,仍然保持自己的自定義URL名稱。

+0

但是這看起來更像是解決方法,其中我可以在文檔中找到它,這在django 1.10中是固定的(因爲django 1.9和1.8.8是一樣的)。 – realnot

相關問題