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

前端 未结 3 1381
别跟我提以往
别跟我提以往 2021-01-02 15:33

I have set the following entry in the urls.py

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

but once I go to

相关标签:
3条回答
  • 2021-01-02 15:40

    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

    0 讨论(0)
  • 2021-01-02 15:41

    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.

    0 讨论(0)
  • 2021-01-02 16:00

    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'),
    
    0 讨论(0)
提交回复
热议问题