Django,创建用户后无法登录 [英] Django, can't log in after creating user
问题描述
我在Django Shell中创建了一个用户,并尝试对其进行身份验证,但是不能.返回NoneType
.另外,我检查is_superuser,is_stuff,is_active为True
I created user in the Django shell and tried to authenticate it, but can't. That returns NoneType
. Also, I check that is_superuser, is_stuff, is_active is True
>>> from django.contrib.auth.models import User
>>> from django.contrib.auth import authenticate
>>> User.objects.create(username='user', password='password', email='someemail')
>>> User.objects.all()
>>> <QuerySet [<User: admin>, <User: user>]>
>>> admin
>>> user = authenticate(username='user', password='password')
>>> user
>>> user.is_active
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'is_active'
>>> type(user)
<class 'NoneType'>
>>> admin = authenticate(username='admin', password='wa23sd54fg')
>>> admin
<User: admin>
>>> admin.is_active()
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'bool' object is not callable
>>> admin.is_active
True
>>> user = User.objects.get(pk=2)
>>> user
<User: user>
>>> user.is_active
True
>>> user.is_superuser
True
>>> user.is_staff
True
尝试在管理面板中登录使用时,显示错误:
When tried log in with use in admin panel, it shows the error:
请输入员工帐户的正确用户名和密码.请注意,两个字段都可能区分大小写.
Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive.
推荐答案
You should have used the create_user
method, so that the password was hashed correctly:
User.objects.create_user(username='user', password='password', email='someemail')
如果您希望用户能够访问Django管理员,请同时设置is_staff=True
:
If you want the user to be able to access the Django admin, set is_staff=True
as well:
User.objects.create_user(username='user', password='password', email='someemail', is_staff=True)
You can fix the password for the existing user in the Django shell with set_password
:
user = User.objects.get(username='user')
user.set_password('password')
user.is_staff = True # allow user to log in to Django admin
user.save()
这篇关于Django,创建用户后无法登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!