版本:
1. Python 3.6.2
2. Django 1.11.6
3. 虚拟环境 Virtualenv
网站提供登录、注册等用户认证功能是一个常见的需求。因此,Django 提供了一套功能完整的、灵活的、易于拓展的用户认证系统:django.contrib.auth
Django大佬的官网地址:https://www.zmrenwu.com/post/30/;里面的讲解也很简单易懂
项目源码:https://github.com/xt998/wechatSystem
一. 创建虚拟环境(https://blog.csdn.net/tyt_XiaoTao/article/details/80314528)
二. 创建Django项目(ps:基础,就不多介绍了),确保在settings.py文件里有app name
三. 代码实现
设置模板目录结构
在项目根目录(manage.py 所在目录)新建一个 templates/ 目录,然后在 templates/ 目录下新建一个 wechat目录,用于存放 wechat应用的相关模板文件。然后在 wechat/ 目录下新建一个 register.html 模板文件
配置模板路径
在 settings.py 里设置 templates/ 所在路径,在 settings.py 找到 TEMPLATES 选项
四. 通过邮箱找回用户的密码
效果图:
阿里云邮箱查看邮箱信息:
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True #是否使用TLS安全传输协议(用于在两个通信应用程序之间提供保密性和数据完整性。)
EMAIL_HOST ='smtp.163.com' #发送邮件的邮箱 的 SMTP服务器,这里用了163邮箱
EMAIL_PORT = 25 #发件箱的SMTP服务器端口
EMAIL_HOST_USER = '[email protected]' #发送邮件的邮箱地址
EMAIL_HOST_PASSWORD = 'xxxxx' #发送邮件的邮箱密码(这里使用的是授权码)
EMAIL_FROM = 'x9128168<[email protected]>' #收件人看到的发件人
views.py
# 在最顶部引入
from django.core.mail import send_mail
from django.conf import settings
def send(request):
if request.method == 'POST':
email = request.POST.get('email')
print(email)
#tulps = eval(email)
#print(tulps)
msg='你收到这封邮件是因为你请求重置你在网站 127.0.0.1:8000上的用户账户密码。请访问该页面并选择一个新密码:<a href="http://127.0.0.1:8000/wechat/reset/NA/4n8-64ab7ff92254d18c6b15/">http://127.0.0.1:8000/users/reset/NA/4n8-64ab7ff92254d18c6b15/</a><br/>你的用户名,如果已忘记的话: admin</br>感谢使用我们的站点!<br/>127.0.0.1:8000 团队'
send_mail('测试邮件01',
msg,
settings.EMAIL_FROM,
[email])
return render(request, 'registration/password_reset_done.html')
templates
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>重置密码</title>
<link rel="stylesheet" href="https://unpkg.com/mobi.css/dist/mobi.min.css">
<style>
.errorlist {
color: red;
}
</style>
</head>
<body>
<div class="flex-center">
<div class="container">
<div class="flex-center">
<div class="unit-1-2 unit-1-on-mobile">
<h1><a href="{% url 'index' %}">Django Auth Example</a></h1>
<h3>重置密码</h3>
<form class="form" action="{% url 'sendMail' %}" method="post">
{% csrf_token %}
{{ form.non_field_errors }}
{% for field in form %}
{{ field.label_tag }}
{{ field }}
{{ field.errors }}
{% if field.help_text %}
<p class="help text-small text-muted">{{ field.help_text|safe }}</p>
{% endif %}
{% endfor %}
<button type="submit" class="btn btn-primary btn-block">提交</button>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
ps:编程小白 还望大佬多多指点