2

Is it possible to pass a flash message along with a path in urls.py?

Since I did not write the auth_views into my core views.py file I figured there might be an easy way to pass it along with the request. I'd prefer to use the auth_views out of the box so that it's easy to update my version of Django. If this is not possible, then I am not trying to force it. Checked the documentation but did not find anything about messages.

path('logout/', auth_views.logout, {'next_page': auth_views.login}, name='logout'),

I'd like to pass something like the message below so that it doesn't feel like they clicked login by accident or something.

'messages.success(request, 'You have securely logged out of {{request.user.email}}. Thank you for visiting.')

Kermit
  • 4,922
  • 4
  • 42
  • 74

1 Answers1

1

Django has a signal which you should be able to hook into in order to do this;

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, user, **kwargs):
    if user:
        msg = 'You have securely logged out {email}. Thank you for visiting.'.format(email=user.email)
    else:
        msg = 'You have securely logged out. Thank you for visiting.'
    messages.add_message(request, messages.INFO, msg)
markwalker_
  • 12,078
  • 7
  • 62
  • 99
  • Thank you =) I am working on this now. Funny thing is that the user object is no longer available by the time the message tries to run so `else:` is running. – Kermit Mar 21 '18 at 01:04
  • @HashRocketSyntax I thought that might be the case because this is after the logout. – markwalker_ Mar 21 '18 at 07:31