pythondjangodjango-logindjango-logging

how to redirect different login page according to type of users specified in django admin?


i created user -

  1. user 1 assigned group A
  2. user 2 assigned group B now i created login page and after login as user1 i want to redirect to page1.html and if logged in as user2 then redirect to page2.html for this i wrote
def login(request):
    if request.method == 'POST': 
        user = auth.authenticate(username=request.POST['username'], password=request.POST['password'])
        if user is not None:
            auth.login(request,user)
            if user.usergroup=='A':
                return redirect('pages/page1.html')
            else:
                return redirect('pages/page2.html')
        else:
            messages.error(request,'Invalid credentials')
            return redirect('login')
            # User is authenticate  
    else:
        return render(request,'pages/login.html')

with this i logged in as user1 ,got the error AttributeError at / 'User' object has no attribute 'usergroup' kindly help is there any other code too ?


Solution

  • try this

    if user.groups.filter(name='A').exists():
        return redirect('pages/page1.html')
    else:
        return redirect('pages/page2.html')