1、确认密码

1.1类
class AdminModelsForm(BootStrapModelForm):
    '''管理员'''
    confirm_password = forms.CharField(
        label="确认密码",
        widget=forms.PasswordInput(render_value=True)
    ) 

    class Meta:
        model = models.Admin
        fields = ["username", "password", "confirm_password"]
        widgets = {
            # render_value=True输入不一致密码后,不清空输入框的密码
            "password": forms.PasswordInput(render_value=True),
        }
1.2钩子函数验证密码是否一致
    clean_表单名
    def clean_confirm_password(self):
        pwd = self.cleaned_data.get("password")
        confirm = md5(self.cleaned_data.get("confirm_password"))
        if pwd != confirm:
            raise ValidationError("密码不一致")

        return confirm

2、添加管理员

def admin_add(request):
    '''添加管理员'''
    title = "新建管理员"
    if request.method == "GET":
        form = AdminModelsForm()
        return render(request, "change.html", {"form": form, "title": title})
    form = AdminModelsForm(data=request.POST)
    if form.is_valid():
        # print(form.cleaned_data)
        form.save()
        return redirect("/admin/list")

    return render(request, "change.html",  {"form": form, "title": title})

3、重置密码

def admin_reset(request, nid):
    '''重置管理员密码'''
    row_object = models.Admin.objects.filter(id=nid).first()
    if not row_object:
        return redirect("/admin/list")
    title = "重置管理员密码-{}".format(row_object.username)
    if request.method == "GET":
        
        form = AdminResetModelsForm()
        return render(request, "change.html", {"form": form, "title": title})
    form = AdminResetModelsForm(data=request.POST, instance=row_object)
    if form.is_valid():
        form.save()
        return redirect("/admin/list")
    return render(request, "change.html", {"form": form, "title": title})

4、MD5加密密码

#加密函数
from django.conf import settings
import hashlib

def md5(data_string):
    salt = settings.SECRET_KEY
    obj = hashlib.md5(salt.encode('utf-8'))
    obj.update(data_string.encode('utf-8'))
    return obj.hexdigest()

#针对加密密码进行验证
class AdminResetModelsForm(BootStrapModelForm):
    '''重置管理员密码ModelForm'''

    confirm_password = forms.CharField(
        label="确认密码",
        widget=forms.PasswordInput(render_value=True)
        )

    class Meta:
        model = models.Admin
        fields = ["password",'confirm_password']
        widgets = {
            # render_value=True输入不一致密码后,不清空输入框的密码
            "password": forms.PasswordInput(render_value=True),
            }


    def clean_password(self):
        '''passowrd勾子函数'''
        pwd = self.cleaned_data.get("password")
        md5_pwd = md5(pwd)
        exists = models.Admin.objects.filter(id=self.instance.pk, password=md5_pwd).exists() 
        #self.intance.pk获取admin_reset中待重置行的管理员ID
        if exists:
            raise ValidationError("不能使用已经用过的密码")
        return md5_pwd
    

    def clean_confirm_password(self):
        pwd = self.cleaned_data.get("password")
        confirm = md5(self.cleaned_data.get("confirm_password"))
        if pwd != confirm:
            raise ValidationError("密码不一致")

        return confirm

VIRIYA
1 声望0 粉丝