在对应的models.py中的User类中添加save方法
1 from django.contrib.auth.hashers import make_password, check_password
1 class User(models.Model): 2 ''' 3 用户 4''' 5 def __str__(self): 6 return self.name 7 class Meta: 8 verbose_name = '用户信息' 9 verbose_name_plural = '用户信息' 10 ACTIVES = (('active','活跃'),('non_active','非活跃')) 11 username = models.CharField('用户名',max_length=12, primary_key=True) 12 password = models.CharField('密码',max_length=10) 13 usergroup = models.ForeignKey('UserGroup',verbose_name="用户组",on_delete=models.PROTECT,blank=True,null=True) 14 is_active = models.CharField('是否活跃',max_length=32,default="active",choices=ACTIVES) 15 16 def save(self, *args, **kwargs): 17 self.password = make_password(self.password, None, 'pbkdf2_sha256') 18 super(User, self).save(*args, ** kwargs)
在对应的views.py中用户登录校验密码时
1 from django.contrib.auth.hashers import check_password
1 name = request.GET.get("name","") 2 pwd = request.GET.get("password","") 3 user = User.objects.get(username=name) 4 if check_password(pwd, user.password): 5 print("密码正确!")