How can I rediredct the views with different user groups in django

154 views Asked by At

This is the view which was written for my django project.

if user is not None:
      if user.is_active:
          auth_login(request, user)
          return HttpResponseRedirect('/home/')
      else:
           messages.error(self.request,
                           _("User is not Active"))
           return HttpResponseRedirect('/')
else:
      messages.error(self.request,_("User Does not Exist"))
      return HttpResponseRedirect(settings.LOGIN_URL)

Suppose there is 3 groups of users customer,admin and super admin. How can I redirect the views to different html for each of the user groups? Thank You

1

There are 1 answers

0
Exprator On BEST ANSWER
if user.groups.all()[0].name == "groupname":
   return redirect('some view')

you can do it like this

or if the user has many groups

l = request.user.groups.values_list('name',flat=True)

if "groupname" in l:
    return redirect('some view')