一 编写模板文件
1 mysite/templates/account/password_reset_form.html
{% extends "base.html" %}
{% block title %}重置密码{% endblock %}
{% block content %}
忘记密码?请重置。
输入您的电子邮件来设置新密码。
{% endblock %}
2 mysite/templates/account/password_reset_email.html
因为你在 CSDN重置了密码,所以你收到了这封邮件!
请转到以下页面,选择一个新密码:
{{ protocol }}://{{ domain }}{% url 'account:password_reset_confirm' uidb64=uid token=token %}
你的用户名是:{{ user.get_username }}
感谢您使用我们的网站!
https://blog.csdn.net/chengqiuming/
3 mysite/templates/account/password_reset_subject.txt
请重置你的密码!
4 mysite/templates/account/password_reset_done.html
{% extends "base.html" %}
{% block title %}重置密码{% endblock %}
{% block content %}
重置密码
我们已经通过电子邮件向您发送了设置密码的指示,如果您输入的电子邮件存在。你应该马上收到。
如果您没有收到电子邮件,请确保您已经输入了您注册的地址,并检查您的垃圾邮件文件夹。
{% endblock %}
5 mysite/templates/account/password_reset_confirm.html
{% extends "base.html" %}
{% block title %}重置密码{% endblock %}
{% block content %}
重置密码
请输入您的新密码两次,以便我们能够验证您输入的是正确的。
{% endblock %}
6 mysite/templates/account/password_reset_complete.html
{% extends "base.html" %}
{% block title %}密码重置{% endblock %}
{% block content %}
重置密码
您的密码已设置,请登录.
{% endblock %}
二 编写URL——mysite/account/urls.py
from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
# 自定义登录
# url(r'^login/$', views.user_login, name='user_login'),
# django内置的登录
url(r"^login/$", auth_views.login, name="user_login"),
url(r"^new-login/$", auth_views.login, {"template_name": "account/login.html"}),
url(r'^logout/$', auth_views.logout, {"template_name": "account/logout.html"}, name='user_logout'),
url(r'^register/$', views.register, name="user_register"),
# 给password_change方法传入post_change_redirect参数
url(r'^password-change/$', auth_views.password_change, {"post_change_redirect": "/account/password-change-done"},
name='password_change'),
url(r'^password-change-done/$', auth_views.password_change_done, name='password_change_done'),
# 重置密码开始
url(r'^password-change/$', auth_views.password_change, {"post_change_redirect":"account/password-change-done"}, name='password_change'),
url(r'^password-change-done/$', auth_views.password_change_done, name='password_change_done'),
url(r'^password-reset/$', auth_views.password_reset, {"template_name":"account/password_reset_form.html","subject_template_name":"account/password_reset_subject.txt", "email_template_name":"account/password_reset_email.html", "post_reset_redirect":"/account/password-reset-done"}, name="password_reset"),
url(r'^password-reset-done/$', auth_views.password_reset_done, {"template_name":"account/password_reset_done.html"}, name="password_reset_done"),
url(r'^password-reset-confirm/(?P[-\w]+)/(?P[-\w]+)/$', auth_views.password_reset_confirm, {"template_name":"account/password_reset_confirm.html", "post_reset_redirect":"/account/password-reset-complete"}, name="password_reset_confirm"),
url(r'^password-reset-complete/$', auth_views.password_reset_complete, {"template_name":"account/password_reset_complete.html"}, name="password_reset_complete"),
# 重置密码结束
]
三 配置邮箱服务器——mysite/mysite/settings.py
EMAIL_HOST = 'smtp.qq.com'
EMAIL_HOST_USER = "[email protected]"
EMAIL_HOST_PASSWORD = "从qq邮箱获得的授权码"
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = "[email protected]"
四 测试
1 浏览器输入:http://localhost:8000/account/password-reset/,然后输入邮箱
2 点击发送电子邮件
3 打开电子邮件
4 进入电子邮件中的链接
5 输入要重置的密码
6 修改密码