1

I have this url:

url(r'^signup/$', TemplateView.as_view(template_name='users/signup.html')),

and I want the user to be redirected to a different url, if they try access this page and are already logged in.

Is there anyway I can do this in the urls.py or will I have to write a view for it?

cbuch1800
  • 923
  • 3
  • 11
  • 26
wtreston
  • 1,051
  • 12
  • 28

2 Answers2

1

I'm not aware of a way to do it in urls.py and so would do this in the view.

You can check using this:

if request.user.is_authenticated: # Do something

You can also do this in the template:

{% if user.is_authenticated %}

This question may be helpful: How to check if a user is logged in (how to properly use user.is_authenticated)?

cbuch1800
  • 923
  • 3
  • 11
  • 26
1

You can use HttpResponseRedirect

Something like this

from django.http import HttpResponseRedirect

def signup(request): # this is your signup view
    if request.user.is_authenticated:
        return HttpResponseRedirect('/dashboard/')

And looks like there's also a redirect shortcut (which ultimately also does an HttpResponseRedirect).

slider
  • 12,810
  • 1
  • 26
  • 42