Question

For the login I usually use the built-in view:

(r'^login/$', 'django.contrib.auth.views.login'),

However I noticed today, If I manually go to myserver.com/login after an already successful login, I get to see the login page again.

This is not consistent. One way to solve this, was if I could have these two lines on top of the login view function:

if request.user.is_authenticated():
    return HttpResponseRedirect('/')  

So that if the user is already authenticated, please redirect to /.

Is there a way to achieve this while still using 'django.contrib.auth.views.login' for login?

UPDATE:

It seems someone had this question already: Django: Redirect logged in users from login page

However the solution doesn't seem to work any longer in Django 1.5

Within the custom login it throws an exception at:

return login(request)

-->

AttributeError: 'AnonymousUser' object has no attribute 'backend'
Was it helpful?

Solution

This solution in my edit works after all.

I had imported the wrong namespace, instead of

from django.contrib.auth.views import login

I had imported:

from django.contrib.auth import login

I hope this helps someone else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top