Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the built-in 'password_reset' view in Django?

I have set the following entry in the urls.py

(r'^password_reset/$', 'django.contrib.auth.views.password_reset'),

but once I go to http://127.0.0.1:8000/password_reset/ I get the error message:

NoReverseMatch at /password_reset/
Reverse for 'django.contrib.auth.views.password_reset_done' with arguments '()' and keyword arguments '{}' not found.

I was expecting password_reset_done view also to be coming out of the box. So what am I supposed to do at this stage?

UPDATE

After trying Blair's solution, I got a step closer.

(r'^password_reset_done/$', 'django.contrib.auth.views.password_reset_done'),

According to the book 'Django 1.0 Website Development', these built-in views should be used out of the box without further hassle. But maybe it has changed since Django 1.0... Would be great if someone could shed light on this. Thanks

like image 403
Houman Avatar asked May 12 '12 22:05

Houman


3 Answers

I have finally found the solution. I think there is always the slight misunderstanding between MVC and MTV pattern. In MTV (Django) the View stands for the controller and the Template stands for the View.

Hence while its true that the change password "Views" are coming built-in out-of-the-box, the actual templates (look & feel) still needs to be generated by the user while the underlying form (widget) is generated by Django automatically. It gets more clear when looking at the code.

Therefore add these two lines to url.py

(r'^change-password/$', 'django.contrib.auth.views.password_change'), 
(r'^password-changed/$', 'django.contrib.auth.views.password_change_done'),

Then Under myproject/templates/registration add these two files

password_change_done.html

{% extends "base.html" %}
{% block title %}Password Change Successful{% endblock %}
{% block head %}Password Change Completed Successfully{% endblock %}
{% block content %}
    Your password has been changed successfully. Please re-login with your new credentials 
    <a href="/login/">login</a> or go back to the
    <a href="/">main page</a>.
{% endblock %}

password_change_form.html

{% extends "base.html" %}
{% block title %}Change Registration{% endblock %}
{% block head %}Change Registration{% endblock %}
{% block content %}
    <form method="post" action=".">
        {{form.as_p}}
        <input type="submit" value="Change" />
        {% csrf_token %}
    </form>
{% endblock %}

enter image description here

like image 180
Houman Avatar answered Sep 19 '22 12:09

Houman


Django needs to know which URL to redirect the user to once they have completed the form on the password_reset page. So add another line to your URL configuration:

(r'^password_reset_done/$', 'django.contrib.auth.views.password_reset_done'),
like image 27
Blair Avatar answered Sep 22 '22 12:09

Blair


As of django 1.11 password_change view is deprecated.

Deprecated since version 1.11: The password_change function-based view should be replaced by the class-based PasswordChangeView.

What worked for me was:

In urls.py

from django.contrib.auth import views as auth_views
...
url('^account/change-password/$',
    auth_views.PasswordChangeView.as_view(
        template_name='registration/passwd_change_form.html'),
    name='password_change'),
url(r'^account/password-change-done/$',
    auth_views.PasswordChangeDoneView.as_view(
        template_name='registration/passwd_change_done.html'),
    name='password_change_done'),

And then add the couple of templates passwd_change_form.html and passwd_change_done.html under registration.

Note that I'm not using the default name, for some reason when I did that it defaulted to the django admin views.

like image 30
Iker Avatar answered Sep 20 '22 12:09

Iker