django auth:authenticate() 出现奇怪的错误

发布于 2024-09-04 16:26:28 字数 1048 浏览 5 评论 0原文

我正在使用authenticate() 手动验证用户身份。 没有“last_login”属性

使用管理界面,我可以看到用户调试回溯

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'

:我不明白,为什么会出现这种差异。
任何形式的帮助将不胜感激。提前致谢!

I am using authenticate() to authenticating users manually.
Using admin interface I can see that there is no 'last_login' attribute for Users

Debug traceback is :

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'

I cant figure out, why is there this discrepancy.
Any kind of help would be appreciated. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七秒鱼° 2024-09-11 16:26:28

问题不在于 authenticate(),它似乎在于 login(),您似乎将 unicode 传递给它,而不是比 django.contrib.auth.models.User 对象。

您可能应该从 authenticate()

user = authenticate(username=username, password=password)
...
login(request, user)

验证并登录 文档

The problem isn't with authenticate(), it seems to be with login() which you appear to be passing a unicode into, rather than a django.contrib.auth.models.User object.

You should probably be getting that User object from authenticate()

user = authenticate(username=username, password=password)
...
login(request, user)

authenticate and login docs

z祗昰~ 2024-09-11 16:26:28

异常值告诉它:“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.pysyncdb?)

请同时发布您的settings.py 的数据库相关部分。根据您当前的信息,很难找到问题的原因。

完整回溯也很有帮助。

The exception value tells it: "user" is a unicode object instead of a django.contrib.auth.models.User object. Are you sure that the database is accessible? try:

python manage.py shell
>>> from django.contrib.auth.models import User
>>> u = User.objects.get(pk=1)
>>> u.last_login

This code must work correctly. If not, then there's something wrong with your database setup. (maybe you did not do python manage.py syncdb ?)

Please post your database related parts of settings.py as well. From your current information it's not easy to find the cause of your problem.

The full traceback is helpful as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文