I made an app and extended AbstractUser to add some fields to my User model. After that, everything works as expected (login, create user, reset password...) but when I try to logout using the default
django.contrib.auth.LogoutView or include('django.contrib.auth.urls')
it will simply ignore the logout. When I go back to the restricted page I can enter and see the content and my user is actually logged in!
I created a custom logout view like this
def custom_logout(request):
print('Loggin out {}'.format(request.user))
auth.logout(request)
print(request.user)
return HttpResponseRedirect('/restrictedpage')
on the restrictedpage I have a print statement to show the user
print("User logged: {}".format(request.user))
When I click logout this is what shows up in the console:
"GET /restrictedpage HTTP/1.1" 200 19820
User logged: ceterre
----- This is where i click logout ------
Loggin out AnonymousUser
AnonymousUser
"GET /accounts/logout/ HTTP/1.1" 302 0 ----- this redirects me to /restrictedpage
User logged: ceterre
"GET /restrictedpage HTTP/1.1" 200 19820
this literally translate to:
- I know ceterre is logged
- logging out ceterre
- user logged: AnonymousUser
- redirect to restricted page (where I should have no access since im supposedly logged out)
- user logged: ceterre (without any login or anything...)
Login and logout is part of Django (and AbstractUser too) so you don't need extend this parts of code. One thing you must do is add in settings.py file this two line of code:
LOGIN_REDIRECT_URL = 'template_name'
LOGOUT_REDIRECT_URL = 'template_name'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With