Problem with login()

18 views
Skip to first unread message

bcrem

unread,
Jun 9, 2008, 2:21:08 PM6/9/08
to Django users
Hello,

I'm trying to set up a user login form; that's working fine, however
when I navigate after login to my home page and try to do a simple
"Hello, <username>" I'm getting a False from user.is_authenticated().

Here're some relevant snippets:

From the login handler:

uName = request.POST['username']
uPass = request.POST['password']
user = authenticate(username=uName, password=uPass)

if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect(next)
else:
message = 'Account Deactivated'


From the index view handler:

user = get_user(request)

if user.is_authenticated():
message = 'Welcome back!'
else:
message = 'Welcome.'

What am I missing here? The index() function is in a separate view
file; however even if I move it to the same file as the login handler,
I get the same behavior. Do I need to do something beyond calling
login() to make the user persistant across all request contexts?

Thanks in advance for any info...

mail....@gmail.com

unread,
Jun 9, 2008, 2:29:00 PM6/9/08
to Django users
what happens when you if you try
user = request.user

or (better yet)

if request.user.is_authenticated():
...

bcrem

unread,
Jun 9, 2008, 2:47:36 PM6/9/08
to Django users
Sorry, get_user(request) was a blind alley; I'm actually using 'user =
request.user' right now, and my first attempt was 'if
request.user.is_authenticated():' as you suggest. Behavior's the
same.

On Jun 9, 2:29 pm, "bax...@gretschpages.com" <mail.bax...@gmail.com>
wrote:

Alex Koshelev

unread,
Jun 9, 2008, 3:02:48 PM6/9/08
to Django users
Check if your cookies are enabled.

mail....@gmail.com

unread,
Jun 9, 2008, 3:19:20 PM6/9/08
to Django users
And caching

erincarter

unread,
Jun 10, 2008, 10:36:44 AM6/10/08
to Django users
I am curious about this as well.

I've been using the Django Registration App (http://code.google.com/p/
django-registration/). I have an almost identical "Hello,
{{ user.username }}" in the base template. Once logged in,
user.is_authenticated() works fine (shows the user name) on all the
registration app views ('r'^password','r'^register') however all of my
other app views using user.is_authenticated() shows as False and no
user name is displayed.

I haven't spent a much time researching the scenario but a solution
would be greatly appreciated.


Thanks for posting this bcrem.

erincarter

unread,
Jun 10, 2008, 11:03:41 AM6/10/08
to Django users
I'm curious about this as well.

I've been using the Django Registration App (http://code.google.com/p/
django-registration/) and experiencing a similar issue. I have an
almost identical method of "Hello, {{ user.username }}" in my base
template. When a user has logged in and on any of the registration
apps views ( 'r'^password/', 'r'^register/' ) the username displays
correctly. However when the user navigates to my other apps views the
username does not show up.

I haven't spent much time researching the scenario but a solution to
the issue would be greatly appreciated.

Thanks for posting this bcrem.

On Jun 9, 1:21 pm, bcrem <billc...@gmail.com> wrote:

Adi

unread,
Jun 10, 2008, 1:18:44 PM6/10/08
to Django users
I would check a couple of things.
1. Your session middleware is installed and configured.
2. 'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',

are in your installed apps

3. if your session mgr implementation is database based, which is the
case by default, that the sessions are created correctly and stored in
the appropriate table. AS an alternate, try changing your sessions to
memory based.

bcrem

unread,
Jun 11, 2008, 1:00:21 AM6/11/08
to Django users
Baxter - do you mean browser caching, or Django caching? Don't know
how to do the latter (yet), so whatever the default is for this -
that's what I've got.

Thanks for all the replies, people. Still working on the user
authentication aspect; seems like I'm zeroing in on the right
functionality, but sometimes the behavior I'm seeing is so arbitrary I
begin to suspect possession by an evil spirit...


On Jun 9, 3:19 pm, "bax...@gretschpages.com" <mail.bax...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages