Django 找回密码

Django 找回密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import random
from django.shortcuts import render
from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password

def findpassword(request):
button = "获取验证码"
new_password = False

if request.method == "POST":
username = request.POST.get("username",'root')
VerificationCode = request.POST.get("VerificationCode","")
password= request.POST.get("password","")
user = User.objects.filter(username=username)
if not user:
tips = f"用户{username}不存在"
else:
if not request.session.get("VerificationCode",""):
button = "重置密码"
tips = "验证码已发送"
new_password = True
VerificationCode = str(random.randint(1000,9999))
request.session["VerificationCode"] = VerificationCode
user[0].email_user("找回密码",VerificationCode)
elif verificationCode == request.session.get("verificationCode"):
dj_ps = make_password(password,None,"pbkdf2_sha256")
user[0].password = dj_ps
user[0].save()
del request.session["VerificationCode"]
tips = "密码已重置"
else
tips = "验证码错误,请重新获取"
new_password = False
def request.session["VerificationCode"]
return render(request,"user.html",locals())

用户model的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
# models.py


from django.db import models
from django.contrib.auth.models import AbstractUser

class MyUser(AbstractUser):
qq = models.ChardField("QQ 号码", max_length=16)
weChat = models.ChardField("微信", max_length=100)
mobile = models.ChardField("手机号码", max_length=11)

def __str__(self):
return self.username

扩展MyUser Admin 信息

1
2
3
4
5
6
7
8
9
from .models import MyUser
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import gettext_lazy as _

@admin.register(MyUser)
class MyUserAdmin(UserAdmin):
list_display = ["uername","email","mobile","qq","weChat"]
fieldsets = list("UserAdmin.fielsets")
fieldsets[1] = (_("Personal info"),{"fields": ("first_name","last_name","email","mobile","qq","weChat"))})

Admin后台app设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# __init__.py

from django.apps import AppConfig
import os

## 修改app在admin后台显示名称
## default_app_config的值来自apps.py类名

default_app_config = "user.IndexConfig"

# 获取当前app的命名
def get_current_app_name(_file):
return os.path.split(os.path.dirname(_file_)[-1])


# 重写类 IndexConfig
class IndexConfig(AppConfig):
name = get_current_app_name(__file__)
verbose_name = "用户管理"

本文标题:Django 找回密码

文章作者:shuke

发布时间:2020年04月20日 - 14:04

最后更新:2020年04月20日 - 14:04

原始链接:https://shuke163.github.io/2020/04/20/Django-%E6%89%BE%E5%9B%9E%E5%AF%86%E7%A0%81/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------

本文标题:Django 找回密码

文章作者:shuke

发布时间:2020年04月20日 - 14:04

最后更新:2020年04月20日 - 14:04

原始链接:https://shuke163.github.io/2020/04/20/Django-%E6%89%BE%E5%9B%9E%E5%AF%86%E7%A0%81/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%