django message when logout

前端 未结 5 2020
青春惊慌失措
青春惊慌失措 2020-12-18 04:24

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

5条回答
  •  一整个雨季
    2020-12-18 04:51

    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.')
    

提交回复
热议问题