Python Can';t在shell中验证Django用户
由于某些原因,我无法在Django shell中对新创建的用户进行身份验证,我真的不知道出了什么问题。一定是很愚蠢的事 启动Django shell:Python Can';t在shell中验证Django用户,python,django,Python,Django,由于某些原因,我无法在Django shell中对新创建的用户进行身份验证,我真的不知道出了什么问题。一定是很愚蠢的事 启动Django shell: (venv) xxx:xxx xxx$ python ../manage.py shell Python 3.6.0 (v3.6.0, ...) [GCC 4.2.1 (...) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more inform
(venv) xxx:xxx xxx$ python ../manage.py shell
Python 3.6.0 (v3.6.0, ...)
[GCC 4.2.1 (...) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
创建新用户:
>>> from django.contrib.auth import get_user_model
>>> email = "testemail@gmail.com"
>>> password = "testpassword"
>>> user = get_user_model().objects.create_user("TestUser", email, password)
新用户似乎已正确添加到数据库:
>>> get_user_model().objects.all()
<QuerySet [<MyUser: testemail@gmail.com>]>
>>> u = get_user_model().objects.first()
>>> u.username
'TestUser'
>>> u.email
'testemail@gmail.com'
>>> u.password
'pbkdf2_sha256$36000$Onrvxuy09b6r$sCuz/2/bIbeg5j7cfO934kCIvzVdxqo1s1v6x6nwYLY='
任何帮助都将不胜感激
以下选项也不起作用:
>>> user = authenticate(username="TestUser", password=password)
>>> user.email
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'email'
>>>
>user=authenticate(username=“TestUser”,password=password)
>>>user.email
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“非类型”对象没有属性“电子邮件”
>>>
您正在使用is\u active=False创建用户。默认的modelbend
阻止非活动用户进行身份验证
如果希望允许非活动用户进行身份验证,可以启用AllowAllUsersModelBackend
后端。或者,如果您只是尝试在shell中测试身份验证
,则首先设置user.is\u active=True
user = get_user_model().objects.create_user("TestUser", email, password)
user.is_active = True
user.save()
有关更多信息,请参阅上的文档。精彩推荐,谢谢!我知道这一点,但完全错过了,我无能为力。
>>> user = authenticate(username="TestUser", password=password)
>>> user.email
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'email'
>>>
user = get_user_model().objects.create_user("TestUser", email, password)
user.is_active = True
user.save()