Password reset in Django 1.0

Check out the new site at https://rkblog.dev.

Resetting password changed in 1.0, and the documentation for this is missing. The basic way of using new views is to add this set to urlconf:

import django.contrib.auth.views
....
(r'^password_reset/$', 'django.contrib.auth.views.password_reset'),
(r'^password_reset/done/$', 'django.contrib.auth.views.password_reset_done'),
(r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm'),
(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete'),
Default templates are in django/contrib/admin/templates/registration and can be used out of the box, but they have Admin Panel like look. You can set paths to your custom templates (copy & edit the original ones):
(r'^password_reset/$', 'django.contrib.auth.views.password_reset', {'template_name':'userpanel/password_reset_form.html', 'email_template_name':'userpanel/password_reset_email.html'}),
(r'^password_reset/done/$', 'django.contrib.auth.views.password_reset_done', {'template_name':'userpanel/password_reset_done.html'}),
(r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm', {'template_name':'userpanel/password_reset_confirm.html'}),
(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete', {'template_name':'userpanel/password_reset_complete.html'}),
If you want to use those views wrapped in your own views, then you will get few NoReverseMatch: Reverse for 'yourapp.django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments... exceptions. You have to pass to the views also a post_reset_redirect argument with URLs to other password reset views:
  • password_reset - URL for password_reset_done view
  • password_reset_confirm - URL for password_reset_complete view
  • password_reset_email.html - You have add in some way URL to password_reset_confirm in URL tag:
    {% url django.contrib.auth.views.password_reset_confirm uidb36=uid, token=token %}
    
Example:
def password_reset(request):
	"""
	django.contrib.auth.views.password_reset view (forgotten password)
	"""
	if not request.user.is_authenticated():
		return django.contrib.auth.views.password_reset(request,
                          template_name='userpanel/password_reset_form.html',
                          email_template_name= 'userpanel/password_reset_email.html',
                          post_reset_redirect='/user/password_reset/done/')
	else:
		return HttpResponseRedirect("/user/")
For the links in the email domain name from Sites app is used. If you change the domain in Admin Panel/Sites, then you will have to restart the server to see the changes in emails.

RkBlog

Django web framework tutorials, 26 September 2008


Check out the new site at https://rkblog.dev.
Comment article
Comment article RkBlog main page Search RSS Contact