0

I would like to be able to render a different logged out template when the user_logged_out signal is fired. I can catch the signal and check for my condition correctly, and I have a view with a named URL that works just fine, but I can't seem to render the view.

I've tried each of these, with both a class based and functional view, but can't get them to work. Using ipdb I can get a template to render in the console, but can't figure out the right way to return it/call the view to have it returned. Thoughts?

@receiver(user_logged_out)
def my_logged_out_signal_handler(sender, request, user, **kwargs):
    if user.has_condition:
        # tried this
        resolve(reverse('my_named_url', kwargs={'kwarg1': 'something'})).func(request, something)
        # and this
        render_to_response(resolve(reverse('my_named_url', kwargs={'kwarg1': something})).func(request, kwarg1=something).render())
        # and this
        render(MyClassView.as_view()(request, kwarg1=something))
        # and this
        return (resolve(reverse('my_named_url', kwargs={'kwarg1': something})).func(request, kwarg1=something).render())
        # and this
        return HttpResponse(resolve(reverse('my_named_url', kwargs={'kwarg1': something})).func(request, kwarg1=something).render())
David
  • 247
  • 3
  • 9
  • 1
    http://stackoverflow.com/questions/5315100/django-redirect-after-log-out – dm03514 Mar 16 '15 at 17:45
  • @dm03514 thanks, but that won't work. I need to know the user so I can check a condition, and after logout is too late. I need to hook the signal to know who they are. – David Mar 16 '15 at 17:46
  • I'd rather keep the session clean after logout. The auth.logout does session.flush() (https://github.com/django/django/blob/stable/1.6.x/django/contrib/auth/__init__.py#L107) which seems like something I want to keep doing. – David Mar 16 '15 at 17:52
  • @dm03514 , actually, i think i can make the solution in your link work for my case. thanks! – David Mar 16 '15 at 19:17

2 Answers2

1

A signal handler is not a view, it cannot render/return a response.

You could simply handle logic in your own view, and call or redirect to the auth logout function from there. Something like below..

from django.shortcuts import redirect

def my_logout(request):
    kwargs = {}
    if my_condition:
        kwargs['template_name'] = 'my_template.html'
        kwargs['extra_context'] = ...
    return redirect('logout', **kwargs)
0

Found a clever solution allowing a redirect from anywhere, if you really need to. https://djangosnippets.org/snippets/2541/

David
  • 247
  • 3
  • 9