views:

79

answers:

2

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!

A: 

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.

mawimawi
+2  A: 

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

pycruft
Where are you importing `authenticate` from?
Daniel Roseman
yeah.. found the problem.Thanks
Rohit