官术网_书友最值得收藏!

  • Django 2 by Example
  • Antonio Melé
  • 306字
  • 2021-06-25 21:19:10

Changing password views

We also need our users to be able to change their password after they log in to our site. We will integrate Django authentication views for password change. Open the urls.py file of the account application and add the following URL patterns to it:

# change password urls
path('password_change/',
auth_views.PasswordChangeView.as_view(),
name='password_change'),
path('password_change/done/',
auth_views.PasswordChangeDoneView.as_view(),
name='password_change_done'),

The PasswordChangeView view will handle the form to change the password, and the PasswordChangeDoneView view will display a success message after the user has successfully changed his password. Let's create a template for each view.

Add a new file inside the templates/registration/ directory of your account application and name it password_change_form.html. Add the following code to it:

{% extends "base.html" %}

{% block title %}Change you password{% endblock %}

{% block content %}
<h1>Change you password</h1>
<p>Use the form below to change your password.</p>
<form action="." method="post">
{{ form.as_p }}
<p><input type="submit" value="Change"></p>
{% csrf_token %}
</form>
{% endblock %}

The password_change_form.html template includes the form to change the password. Now, create another file in the same directory and name it password_change_done.html. Add the following code to it:

{% extends "base.html" %}

{% block title %}Password changed{% endblock %}

{% block content %}
<h1>Password changed</h1>
<p>Your password has been successfully changed.</p>
{% endblock %}

The password_change_done.html template only contains the success message to be displayed when the user has successfully changed their password.

Open http://127.0.0.1:8000/account/password_change/ in your browser. If your user is not logged in, the browser will redirect you to the login page. After you are successfully authenticated, you will see the following change password page:

Fill in the form with your current password and your new password, and click on the CHANGE button. You will see the following success page:

Log out and log in again using your new password to verify that everything works as expected.

主站蜘蛛池模板: 寿光市| 旅游| 沁阳市| 资溪县| 新和县| 卢湾区| 建始县| 安康市| 白玉县| 赤城县| 馆陶县| 玛曲县| 丹寨县| 海伦市| 凤冈县| 邵东县| 霍州市| 石阡县| 荃湾区| 栾川县| 商洛市| 凉城县| 赣榆县| 资溪县| 老河口市| 绥芬河市| 临洮县| 当涂县| 麻城市| 淅川县| 二连浩特市| 怀远县| 专栏| 曲阜市| 文安县| 肇庆市| 南城县| 错那县| 东台市| 桐城市| 胶州市|