Redirect user inside Class-Based views

402 views
Skip to first unread message

Rootz

unread,
Dec 2, 2014, 9:17:40 AM12/2/14
to django...@googlegroups.com
I have a small Django App using several Class-Based Views but I am contemplating how the class views would redirect a user based upon a condition? In my case I would like to redirect user who are not authenticated. Just how do I go about doing that?

thanks

Andreas Kuhne

unread,
Dec 2, 2014, 9:22:32 AM12/2/14
to django...@googlegroups.com
2014-12-02 15:17 GMT+01:00 Rootz <arc4...@gmail.com>:
I have a small Django App using several Class-Based Views but I am contemplating how the class views would redirect a user based upon a condition? In my case I would like to redirect user who are not authenticated. Just how do I go about doing that?

thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/57d8f675-5336-421a-9f36-aee6e24545ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


As long as the class based view doesn't require you to be logged in you can achive that by doing:

def dispatch(self, request, *args, **kwargs):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse("url_name_to_redirect_to"))

# Continue with other dispatch functionality

Regards,

Andréas

Rootz

unread,
Dec 2, 2014, 10:10:40 AM12/2/14
to django...@googlegroups.com
thanks

pjotr

unread,
Dec 3, 2014, 10:24:44 AM12/3/14
to django...@googlegroups.com
You could also look into django-braces that has some mixins to handle some common operations.

Rootz

unread,
Dec 3, 2014, 10:59:34 AM12/3/14
to django...@googlegroups.com
Thanks again..
Reply all
Reply to author
Forward
0 new messages