Python django auth:authenticate()出现奇怪错误
我正在使用authenticate()手动验证用户。 使用管理界面,我可以看到用户没有“上次登录”属性 调试回溯是:Python django auth:authenticate()出现奇怪错误,python,django,django-authentication,Python,Django,Django Authentication,我正在使用authenticate()手动验证用户。 使用管理界面,我可以看到用户没有“上次登录”属性 调试回溯是: Environment: Request Method: GET Request URL: https://localhost/login/ Django Version: 1.1.1 Python Version: 2.6.5 Installed Applications: ['django.contrib.auth', 'django.contrib.contenttyp
Environment:
Request Method: GET
Request URL: https://localhost/login/
Django Version: 1.1.1
Python Version: 2.6.5
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'mobius.polls']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')
Traceback:
File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py" in get_response
92. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/pymodules/python2.6/django/contrib/auth/__init__.py" in login
55. user.last_login = datetime.datetime.now()
Exception Type: AttributeError at /login/
Exception Value: 'unicode' object has no attribute 'last_login'
我不明白,为什么会有这种差异。任何形式的帮助都将不胜感激。提前谢谢 异常值告诉它:“user”是一个unicode对象,而不是django.contrib.auth.models.user对象。您确定数据库是可访问的吗?尝试:
python manage.py shell
>>> from django.contrib.auth.models import User
>>> u = User.objects.get(pk=1)
>>> u.last_login
此代码必须正常工作。如果没有,则说明数据库设置有问题。(可能您没有执行python manage.py syncdb?)
请同时发布settings.py中与数据库相关的部分。从您当前的信息中很难找到问题的原因
full回溯也很有帮助。问题不在于
authenticate()
,而在于login()
,您似乎要将unicode
传递给它,而不是django.contrib.auth.models.User
对象
您可能应该从authenticate()
文档您从哪里导入
身份验证
?是的。。发现了问题。谢谢
user = authenticate(username=username, password=password)
...
login(request, user)