Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
992 views
in Technique[技术] by (71.8m points)

django message when logout

Once an user logged out of the site, it should redirect to the home page and to display the message as "U are successfully logged out" in the top of the page. Anyone help me in displaying message in home page?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use the user_logged_out signal combined with the messages framework:

First, make sure the messages framework is set up (https://docs.djangoproject.com/en/dev/ref/contrib/messages/).

Then include this code somewhere that will be called (I tend to put it in a receivers.py module and then import it from a models.py file in an installed app):

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


@receiver(user_logged_out)
def on_user_logged_out(sender, request, **kwargs):
    messages.add_message(request, messages.INFO, 'Logged out.')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...