2011-03-27 91 views
2

我缺少這裏真正基本的東西。Django反向問題()

我想重用django的更改密碼視圖。我有以下的urls.py:

(r'^change-password/$', 'profile.views.change_password', {},'change_password'), 
url(r'^change-password-done/$', 'profile.views.password_change_done', name='django.contrib.auth.views.password_change_done'), 

和相應的views.py:

from django.contrib.auth.views import password_change, password_change_done 

def change_password(request,template_name="password_change_form.html"): 
    """Change Password""" 
    return password_change(request,template_name=template_name) 

def password_change_done(request, template_name="password_change_done.html"): 
    return render_to_response(template_name,(),context_instance= RequestContext(request)) 

,但我得到以下錯誤:

Reverse for 'django.contrib.auth.views.password_change_done' with arguments '()' and keyword arguments '{}' not found.

看了看源,看到這line:

post_change_redirect = reverse('django.contrib.auth.views.password_change_done') 

如果我改變我的urls.py進入之後,我沒有得到上述錯誤:

url(r'^change-password-done/$', 'django.contrib.auth.views.password_change_done', name='anything'), 

,但我很困惑,逆()應該查找使用「名」參數?我在這裏錯過了什麼?

我使用Django 1.2.3

回答

1

reverse功能不只是做名稱查找。

Reverse Documentation

reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None) viewname is either the function name (either a function reference, or the string version of the name, if you used that form in urlpatterns) or the URL pattern name.

所以,做reverse('django.contrib.auth.views.password_change_done'),它會查找該視圖名稱的正則表達式urls.py內,並退回到尋找name關鍵字參數如果視圖名沒有解決。

+0

這正是我無法理解的文檔中的一部分。當我做'reverse('django.contrib.auth.views.password_change_done')'時,它應該回退到尋找名稱關鍵字參數,當我有'url(r'^ change-password-done/$' 'profile.views.password_change_done',name ='django.contrib.auth.views.password_change_done'), ' – 2011-03-27 09:58:14

2

喬希有解釋,但你做錯了。如果你想overrride的post_save_redirect,然後傳遞作爲一個參數,當你調用視圖:

def change_password(request,template_name="password_change_form.html"): 
    return password_change(request, template_name=template_name, 
          post_save_redirect=reverse('my_done_page')) 

the documentation