I have a signal named user_logged_in
that is dispatched when the user logs in. When the signal is received by post_user_logged_in
and the user has a session variable named do_stuff
set, I want to run do_stuff()
, delete the session variable, and then add a new did_stuff
variable to the template context so I can show a message and take other client-side action. The signal passes the request object as a parameter.
How can I modify the request object to include my new variable in the template context? I tried the below after some Googling but did_stuff
remained blank in the template. I also considered using a custom message but that feels incorrect.
from django.dispatch import receiver
from django.template import RequestContext
from allauth.account.signals import user_logged_in
from myproject import do_stuff
@receiver(user_logged_in)
def post_user_logged_in(sender, request, user, **kwargs):
if 'do_stuff' in request.session:
del request.session['do_stuff']
do_stuff()
request.context = RequestContext(request)
request.context['did_stuff'] = True