django2.2.6中check_password验证失败的解决办法

  介绍

本篇内容介绍了“django2.2.6中check_password验证失败的解决办法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

python3.6, 2.2.6 django。AUTHENTICATION_BACKENDS里添加自定义认证,CustomBackend(邮箱,手机号等),

用,python管理。py createsuperuser创建的超级管理员登录时密码一直验证失败(False)

#。\程序\ \后端用户\ other.py

 # !/usr/bin/env  python3
  #,- *安康;编码:utf-8  - * -
  
  “““
  @author: yinzhuoqun
  @site: http://zhuoqun.info/:yin@zhuoqun.info
  @time: 2019/10/16  18:06
  “““
  得到django.contrib.auth  import  get_user_model
  得到django.contrib.auth.backends  import  ModelBackend
  得到django.db.models  import  Q
  得到django.contrib.auth.hashers  import  check_password
  得到apps.users.models  import  UserProfile
  
  时间=User  get_user_model ()
  
  
  #,用户名之外的唯一值字段也能用来登录,setting 里要有对应的配置,AUTHENTICATION_BACKENDS
  class  CustomBackend (ModelBackend):
  ,,,def 验证(自我,,请求,用户名=没有,,密码=没有,,* * kwargs):
  ,,,,,,,试一试:
  ,,,,,,,,,,,#,邮箱,用户名,手机号码,登录
  ,,,,,,,,,,,user =, UserProfile.objects.get (Q(用户名=用户名),|,Q(邮件=用户名),|,Q(电话=用户名))
  ,,,,,,,,,,,#,user_check_password =, user.check_password(密码)
  ,,,,,,,,,,,user_check_password =, check_password(密码,user.password)
  ,,,,,,,,,,,if  user_check_password:
  ,,,,,,,,,,,,,,,return 用户
  ,,,,,,,except  User.DoesNotExist:
  ,,,,,,,,,,,return 没有

<按钮>复制

# settings.py

 AUTH_USER_MODEL =, & # 39; users.UserProfile& # 39;,, #,重载系统的用户,让,UserProfile 生效
  
  #,AUTH 方法(支持邮箱,手机号等登录),,验证从上到下
  AUTHENTICATION_BACKENDS =, (
  ,,,& # 39;apps.users.backends.other.CustomBackend& # 39;
  ,,,,# & # 39;social_core.backends.weibo.WeiboOAuth3& # 39;
  ,,,,# & # 39;social_core.backends.qq.QQOAuth3& # 39;
  ,,,,# & # 39;social_core.backends.weixin.WeixinOAuth3& # 39;
  ,,,,# & # 39;social_core.backends.github.GithubOAuth3& # 39;
  ,,,,# & # 39;social_core.backends.gitlab.GitLabOAuth3& # 39;
  ,,,,# & # 39;django.contrib.auth.backends.ModelBackend& # 39;
  )

<按钮>复制

删库重建无数次都不行,突然想到用壳重新设置密码一次,果然就登录上去了。

 (tracbug), \ tracbug> python  manage.py 壳
  Python  3.6.3  (v3.6.3:2c5fed8, Oct , 3, 2017,, 18:11:49), [MSC  v.1900  64年,bit  (AMD64)],提醒win32
  想Type “帮助”,,“copyright",,“credits",趁机“license" for  more 信息。
  (InteractiveConsole)
  在祝辞祝辞,得到django.contrib.auth  import  get_user_model
  在祝辞祝辞,User =, get_user_model ()
  在祝辞祝辞,user =, User.objects.get(用户名=皔inzhuoqun")
  在祝辞祝辞用户
  & lt; UserProfile:, Yinzhuoqun>
  在祝辞祝辞user.username
  & # 39;yinzhuoqun& # 39;
  在祝辞祝辞,user.set_password (“xxxxxxxx")
  祝辞祝辞祝辞,user.save () 

django2.2.6中check_password验证失败的解决办法