To stop a redirect back to logout when the login URL ends with ?next=/accounts/logout/ in Django
Asked Answered
C

1

2

In my template I am currently using the next parameter to redirect the user back to the page before the login page with this:

<a href="{% url auth_login %}?next={% firstof request.path '/' %}">Log in</a>

The firstof tag makes sure that in case request.path is invalid, then it will redirect back to the root URL.

This works well on every page except one: the logout page. If I wanted to switch user, then I would first log out, then click log in. But then my url would be

http://127.0.0.1:8000/accounts/login/?next=/accounts/logout/

So as soon as I log in, I would immediately be logged back out again. How do I modify the template so with something like this pseudocode:

if request.path and request.path != reverse( 'auth_logout' )
    return request.path

return "/"
Courbevoie answered 30/3, 2012 at 3:50 Comment(0)
C
2

The easiest solution I can think of, is add "?next=/" to your logout url, that way, as soon as the user logs out, he will be immediately redirected to the specified url, so no one will ever stay on logout page upon logout.

Otherwise you would have to rewrite the login view and add any custom logic you need, which would be easier if contrib.auth views were class views, which they are currently not unfortunately.

So copy paste it and modify :) (I know that copy pasting is bad, but thats the only way you could add custom behavior to it).

The view is located here: https://code.djangoproject.com/browser/django/trunk/django/contrib/auth/views.py#L25

Chaqueta answered 30/3, 2012 at 4:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.