Home > Net >  Redirect User to their corresponding View and Templates in Django
Redirect User to their corresponding View and Templates in Django

Time:09-18

I have 2 different user types at the moment. After a successful login, I want the user to be redirected to its specific dashboard.

Depending on the user, it is possible to load in a different template in a generic view Class:

if request.user.user_type == 1:
    # load template A
if request.user.user_type == 2:
    # load template B

But I want to have two separate View Classes for each Type. How can I achieve this?

CodePudding user response:

use this:

HttpResponseRedirect("/dashboard/")

CodePudding user response:

You can override get_template_names in a Class Based View that inherits the TemplateResponseMixin

   def get_template_names(self):
        if request.user.user_type == 1:
            return ['template1.html']
        else:
            return ['template2.html']

Note that the function is expected to return a list, not just the name of a template. Above it is just returning a single item list.

CodePudding user response:

Create Two views for both templates and URLs than simply redirect:

if request.user.user_type == 1:
   return redirect("TemplateA")
if request.user.user_type == 2:
   return redirect("TemplateB")
  • Related