Is there a way to redirect to another page in Django when outside of a view function.
I need to be able to redirect anywhere in my app, NOT just when I’m returning an HTTP response back to the client.
I’ve tried the redirect shortcut:
from django.shortcuts import redirect
redirect("/some/url")
and also
redirect("http://someserver.com/some/url")
but this does no noticable action.
I need something like header("Location: /some/url"); in PHP which can be used anywhere inside the script.
Thanks
You could abuse/leverage
process_exceptionof middleware:You can then do:
Also don’t forget to add
RedirectMiddlewareto yourMIDDLEWARE_CLASSESsettings.