tread
tread

Reputation: 11098

Django How to add a logout successful message using the django.contrib.auth?

I am not using all-auth

I am using the standard authentication system and url's provided by django.contrib.auth.

I have also ensured that when logging out the user is automatically redirected to the login page

LOGOUT_REDIRECT_URL = "login"

I would like to add a message so the user knows they have been logged out like:

from django.contrib import messages

messages.add_message(request, messages.INFO, 'You have been logged out.')

Would I be able to achieve this without making my own view to logout. Could I use signals?

Upvotes: 3

Views: 1528

Answers (1)

Satendra
Satendra

Reputation: 6865

You can use user_logged_out signal

from django.contrib.auth.signals import user_logged_out
from django.contrib import messages

def show_message(sender, user, request, **kwargs):
    # whatever...
    messages.info(request, 'You have been logged out.')

user_logged_out.connect(show_message)

Upvotes: 5

Related Questions