auth.authenticate()一直返回None [英] auth.authenticate() keeps returning None
问题描述
这是我的问题,我想验证一个自定义的AbstractBaseUser。
如果request.POST:
username = request.POST ['username']
password = request.POST ['password']
user = auth.authenticate(username = username,password = password)
打印用户
如果用户不是无:
...
我的用户信息是用户名:汤姆,密码:汤姆。
当我检入shell,我有一个SimpleUser与这些信息,所以它退出。现在当我在django控制台中打印用户时,它会打印无。但是,当我看看Django的信息时,它说
{'username':u'tom',u'csrf_token ':< django.utils.functional .__ proxy__ object at 0x7fbb681fc650>'errors':['Username / password error'],'password':u'tom'}
/ pre>
所以从我看到,用户名和密码是正确的。怎么了?
编辑:创建SimpleUser:
class SimpleUser (abstractBaseUser):
username = models.TextField(max_length = 40,unique = True)
firstname = models.TextField(max_length = 40)
lastname = models.TextField(max_length = 40)
email = models.EmailField()
社会= models.TextField(max_length = 255)
对象= UserManager()
USERNAME_FIELD ='用户名'
REQUIRED_FIELDS = ['密码','社会','电子邮件']
编辑2:在views.py中注册视图:
def registerview(request):
firstname =
lastname =
username =
password01 =
password02 =
email =
社会=
错误= []
hlinks = [(http:// localhost:8000 /,索引),
(http:// localhost:8000 / login,登录),
(http:/ / localhost:8000 / register,Register),]
if request.POST:
firstname = request.POST ['firstname']
lastname = request.POST ['lastname' ]
username = request.POST ['username']
password01 = request.POST ['password01']
password02 = request.POST ['password02']
email =请求.pOST ['email']
社会= request.POST ['社会']
如果(password01!=和password01 == password02和firstname!=和lastname!=,用户名!=和email!=和社会!=):
try:
SimpleUser.objects.get(username = username)
除了SimpleUser.DoesNotExist:
try:
SimpleUser.objects.get(email = email)
除SimpleUser.DoesNotExist外:
u = SimpleUser(firstname = firstname,
lastname = lastname,
用户名= username,
password = password01,
email = email,
社会=社会)
u.save()
返回HttpResponseRedirect('/ login /')
errors.append(
invalide user / pass)
else:
errors.append(fill all fields)
c = {
' headerlinks':hlinks,
'footerlinks':footerlinks,
'firstname':firstname,
'lastname':lastname,
'username':username,
'电子邮件':电子邮件,
'社会':社会,
'errors':错误,
}
c.update(csrf(request))
return jinja_render_to_response 'registerview.jhtml',c)
编辑3:添加我的backends.py:
m模型导入SimpleUser
class SimpleUserAuth(object):
def authenticate(self,username = None,password = None):
try:
user = SimpleUser.objects.get(username = username)
如果user.check_password(密码):
返回用户名
除SimpleUser.DoesNotExist外:
返回无
def get_user(self,user_id):
try:
user = SimpleUser.objects.get(pk = user_id)
如果user.is_active:
返回用户
返回无
除SimpleUser.DoesNotExist外:
返回无
解决方案这不起作用,因为当您创建新用户时,正在提供密码。所以它在数据库中存储为纯文本,而不是哈希值。而当您调用authenticate功能时,它将检查散列值。
在您的注册中,您应该使用objects.create_user
或设置密码为set_password(password)
Here is my problem, I want to authenticate a custom AbstractBaseUser in.
if request.POST: username = request.POST['username'] password = request.POST['password'] user = auth.authenticate(username=username, password=password) print user if user is not None: ...
My user's informations are username: tom, password: tom. When I check in the shell, I have a SimpleUser with these informations, so it exits. Now when I print user in the django console, it prints None. But, when I look at the informations Django has, it says
{'username': u'tom', u'csrf_token': <django.utils.functional.__proxy__ object at 0x7fbb681fc650>, 'errors': ['Username/password error'], 'password': u'tom'}
So from what I see, username and password are correct. What's wrong ?
Edit : Creation of SimpleUser :
class SimpleUser(AbstractBaseUser): username = models.TextField(max_length=40, unique=True) firstname = models.TextField(max_length=40) lastname = models.TextField(max_length=40) email = models.EmailField() society = models.TextField(max_length=255) objects = UserManager() USERNAME_FIELD = 'username' REQUIRED_FIELDS = ['password', 'society', 'email']
Edit 2 : Register view in views.py :
def registerview(request): firstname = "" lastname = "" username = "" password01 = "" password02 = "" email = "" society = "" errors = [] hlinks = [("http://localhost:8000/", "Index"), ("http://localhost:8000/login", "Login"), ("http://localhost:8000/register", "Register"), ] if request.POST: firstname = request.POST['firstname'] lastname = request.POST['lastname'] username = request.POST['username'] password01 = request.POST['password01'] password02 = request.POST['password02'] email = request.POST['email'] society = request.POST['society'] if (password01 != "" and password01 == password02 and firstname != "" and lastname != "" and username != "" and email != "" and society != ""): try: SimpleUser.objects.get(username=username) except SimpleUser.DoesNotExist: try: SimpleUser.objects.get(email=email) except SimpleUser.DoesNotExist: u = SimpleUser(firstname=firstname, lastname=lastname, username=username, password=password01, email=email, society=society) u.save() return HttpResponseRedirect('/login/') errors.append( "invalide user/pass") else: errors.append("fill all fields") c = { 'headerlinks': hlinks, 'footerlinks': footerlinks, 'firstname': firstname, 'lastname': lastname, 'username': username, 'email': email, 'society': society, 'errors': errors, } c.update(csrf(request)) return jinja_render_to_response('registerview.jhtml', c)
Edit 3 : Add my backends.py :
from models import SimpleUser class SimpleUserAuth(object): def authenticate(self, username=None, password=None): try: user = SimpleUser.objects.get(username=username) if user.check_password(password): return username except SimpleUser.DoesNotExist: return None def get_user(self, user_id): try: user = SimpleUser.objects.get(pk=user_id) if user.is_active: return user return None except SimpleUser.DoesNotExist: return None
解决方案This is not working because when you are creating new user you are providing password as it is. So its stored as plain text in database, not as hashed value. And when you call authenticate function it will check against hashed value. In your register you should either use
objects.create_user
or set password withset_password(password)
这篇关于auth.authenticate()一直返回None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!