Problem with Django reverse()
I am missing something really basic here.
I am trying to reuse django's change password views. I have following in 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'),
and in corresponding 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"):
开发者_JS百科 return render_to_response(template_name,(),context_instance= RequestContext(request))
but I am getting following error:
Reverse for 'django.contrib.auth.views.password_change_done' with arguments '()' and keyword arguments '{}' not found.
looked at the source and saw this line:
post_change_redirect = reverse('django.contrib.auth.views.password_change_done')
If I change my urls.py entry to following , I do not get the above error:
url(r'^change-password-done/$', 'django.contrib.auth.views.password_change_done', name='anything'),
but I am confused as reverse() should look-up using the "name" parameter? What am I missing here?
I am using django 1.2.3
Josh has the explanation, but you are doing this wrong. If you want to overrride the post_save_redirect
, then pass that in as a parameter when you call the view:
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'))
See the documentation.
The reverse
function doesn't just do lookups on name.
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.
So, by doing reverse('django.contrib.auth.views.password_change_done')
, it will look up that view name within the urls.py
regexes, and fallback to looking for the name
keyword argument if the viewname doesn't resolve.
精彩评论