Как использовать встроенное в Django управление паролями
Сделаем так, чтобы наши пользователи легко могли управлять своей учетной записью.
Что необходимо
- настроенная почта Django в проекте
- адрес пользователя подтвержденный, чтобы отправить ему сброс пароля
1. Обновление ссылки проекта в urls.py
# yourproject.urls.py
from django.conf.urls import url, include
urlpatterns = [
...
url(r'^accounts/', include('accounts.password.urls')),
...
]
2. Создание модуля управления учетной записью
$ cd /path/to/your/project/root # where manage.py is
$ python manage.py startapp accounts # assuming we want to store our passwords module here.
$ mkdir accounts/passwords
$ touch accounts/passwords/__init__.py
$ touch accounts/passwords/urls.py
3. Создание urls.py в модуле управления учетной записью
# accounts.passwords.urls.py
from django.conf.urls import url
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^password/change/$', auth_views.PasswordChangeView.as_view(), name='password_change'),
url(r'^password/change/done/$', auth_views.PasswordChangeDoneView.as_view(), name='password_change_done'),
url(r'^password/reset/$', auth_views.PasswordResetView.as_view(), name='password_reset'),
url(r'^password/reset/done/$', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
url(r'^password/reset/(?P[0-9A-Za-z_\-]+)/(?P[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
auth_views.PasswordResetConfirmView.as_view(),
name='password_reset_confirm'),
url(r'^password/reset/complete/$',
auth_views.PasswordResetCompleteView.as_view(),
name='password_reset_complete'),
]
4. Добавление шаблонов
Шаблоны нужно добавить в <каталог-шаблонов>/registration/
.
password_change_done.html
{% extends "base.html" %}
{% block content %}
<div class='row'>
<div class="col-12 text-center py-5">
<h1 class='my-5 py-5'>Password successfully changed!</h1>
</div>
</div>
{% endblock %}
password_change_form.html
{% extends "base.html" %} {% block content %} <div class='row'> <div class='col-md-4 col-10 mx-auto'> <h1>Change your Password</h1> <form method='post' action=''>{% csrf_token %} {{ form.as_p }} <span style='margin-bottom: 12px;'></span> <button type="submit" class='btn btn-block btn-info'>Save</button> </form> </div> </div> {% endblock %}
password_reset_complete.html
{% extends "base.html" %}
{% block content %}
<div class='row'>
<div class="col-12 text-center py-5">
<h1 class='my-5 py-5'>Password reset complete</h1>
<a href='/login'>Login</a>
</div>
</div>
{% endblock %}
password_reset_confirm.html
{% extends "base.html" %}
{% block content %}
<div class='row'>
<div class='col-md-4 col-10 mx-auto'>
<h1>Set your Password</h1>
<form method='post' action=''>{% csrf_token %}
{{ form.as_p }}
<span style='margin-bottom: 12px;'></span>
<button type="submit" class='btn btn-block btn-info'>Set new password</button>
</form>
</div>
</div>
{% endblock %}
password_reset_done.html
{% extends "base.html" %}
{% block content %}
<div class='row'>
<div class="col-12 text-center py-5">
<h1 class='my-5 py-5'>Rest Instructions Sent</h1>
<p>Please check your email</p>
</div>
</div>
{% endblock %}
password_reset_email.html
{% load i18n %}
{% blocktrans %}
Hello,
Reset your password on {{ domain }} for {{ user }}:
{% endblocktrans %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
password_reset_email.txt
{% load i18n %}
{% blocktrans %}
Hello,
Reset your password on {{ domain }} for {{ user }}:
{% endblocktrans %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
password_reset_form.html
{% extends "base.html" %}
{% block content %}
<div class='row'>
<div class='col-md-4 col-10 mx-auto'>
<h1>Reset your Password</h1>
<form method='post' action=''>{% csrf_token %}
{{ form.as_p }}
<span style='margin-bottom: 12px;'></span>
<button type="submit" class='btn btn-block btn-info'>Reset Password</button>
</form>
</div>
</div>
{% endblock %}