Skip to content

Instantly share code, notes, and snippets.

@aamishbaloch
Created October 31, 2017 13:06
Show Gist options
  • Save aamishbaloch/ea2b3a8186f16c964d7cc174a47e58fc to your computer and use it in GitHub Desktop.
Save aamishbaloch/ea2b3a8186f16c964d7cc174a47e58fc to your computer and use it in GitHub Desktop.
Redirect to another URL in Django Template View

Redirect to another URL in Django Template View

Sometime we might came across the need to redirect to another url from django template. Like if you want to check if user is authenticated or not. So following is the example for doing that in Django.

def dispatch(self, request, *args, **kwargs):
    if not request.user.is_authenticated():
        return redirect('login')

    return super(MyTemplateView, self).dispatch(request, *args, **kwargs)
@RajvirS99
Copy link

I was new to Django and didn't know how can I redirect to another page depending on the specific condition in Django but this helped a lot!
Thanks @aamishbaloch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment