None

Django Reset Password

April 9, 2010

The Django framework comes with support for resetting user passwords. This is implemented for the Admin app, but it is possible to re-use from your own screens.

URLs

The following four URLs are used in the password reset

(r'^accounts/password/reset/$', 'django.contrib.auth.views.password_reset', 
        {'post_reset_redirect' : '/accounts/password/reset/done/'}),
(r'^accounts/password/reset/done/$', 'django.contrib.auth.views.password_reset_done'),
(r'^accounts/password/reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm', 
        {'post_reset_redirect' : '/accounts/password/done/'}),
(r'^accounts/password/done/$', 'django.contrib.auth.views.password_reset_complete'),

Templates

The following five templates are required. These are the templates for the four urls pointed to from the urlpatterns above, plus one template for the email.

  • registration/password_reset_complete.html
  • registration/password_reset_confirm.html
  • registration/password_reset_done.html
  • registration/password_reset_form.html
  • registration/password_reset_email.html

registration/password_reset_complete.html

{% extends "template.html" %}

{% block title %}Password reset complete{% endblock %}

{% block pagetitle %}Password reset complete{% endblock %}

{% block content %}
<p>Your password has been set.  You may go ahead and log in now.</p>
<p><a href="{{ login_url }}">Log in>/a></p>
{% endblock %}

registration/password_reset_confirm.html

{% extends "template.html" %}
{% block title %}Password reset{% endblock %}

{% block pagetitle %}Password reset{% endblock %}

{% block content %}

{% if validlink %}
<p>Please enter your new password twice so we can verify you typed it in correctly.</p>
<form action="" method="post">
  <table>
    <tr>
      <td>{{ form.new_password1.errors }}<label for="id_new_password1">New password:</label></td>
      <td>{{ form.new_password1 }}</td>
    </tr>
    <tr>
      <td>{{ form.new_password2.errors }}<label for="id_new_password2">Confirm password:</label></td>
      <td>{{ form.new_password2 }}</td>
    </tr>
    <tr>
      <td></td>
      <td><input type="submit" value="Change my password" /></td>
    </tr>
  </table>
</form>
{% else %}
<h1>Password reset unsuccessful</h1>
<p>The password reset link was invalid, possibly because it has already been used.  Please request a new password reset.</p>
{% endif %}
{% endblock %}

registration/password_reset_done.html

{% extends "template.html" %}

{% block title %}Password reset successful{% endblock %}

{% block pagetitle %}Password reset successful{% endblock %}

{% block content %}
<p>We've e-mailed you instructions for setting your password to the e-mail address you submitted.</p>
<p>You should be receiving it shortly.</p>
{% endblock %}

registration/password_reset_form.html

{% extends "template.html" %}

{% block title %}Password reset{% endblock %}

{% block pagetitle %}Password reset{% endblock %}

{% block content %}
<p>Forgotten your password? Enter your e-mail address below, and we'll e-mail instructions for setting a new one.</p>

<form action="" method="post">
 {{ form.email.errors }}
<p><label for="id_email">E-mail address:</label> {{ form.email }} <input type="submit" value="Reset my password" /></p>
</form>
{% endblock %}

registration/password_reset_email.html

{% autoescape off %}
You're receiving this e-mail because you requested a password reset for your user account at {{ site_name }}.

Please go to the following page and choose a new password:
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url django.contrib.auth.views.password_reset_confirm uidb36=uid, token=token %}
{% endblock %}

Your username, in case you've forgotten: {{ user.username }}

Thanks for using our site!

The {{ site_name }} team.

{% endautoescape %}

Tags: django reset password