• Django 自定义user model密码加密


    在对应的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("密码正确!")
  • 相关阅读:
    Hadoop的Shuffle阶段
    Java实现单词统计
    SpringBoot学习笔记
    Linux系统管理学习实训任务书
    Java语言学习案例雷霆战机
    PPT文档学习小练习链接
    Word文档学习小练习链接
    学习Java爬虫文档的学习顺序整理
    MapReduce和Hive学习文档链接学习顺序
    [swift]UITableView
  • 原文地址:https://www.cnblogs.com/rn-05181226-rw/p/12418767.html
Copyright © 2020-2023  润新知