Home > Net >  How do I get my django code to redirect users accordingly
How do I get my django code to redirect users accordingly

Time:01-12

I'm want my code to redirect students and teachers to two different pages when the login button is clicked but i kept getting this error: 'AnonymousUser' object has no attribute 'is_teacher'

Here is my login view code:

def login_user(request):
    if request.method=='POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)
            if user is not None:
                if request.user.is_teacher:
                    login(request,user)
                    return redirect('/myapp/home')
                else:
                    login(request, user)
                    return redirect('/myapp/application')
            else:
                messages.error(request,"Invalid username or password")
        else:
            messages.error(request,"Invalid username or password")
    return render(request, 'login.html',context={'form':AuthenticationForm()})

CodePudding user response:

You should check user.is_teacher, since that is the user you are authenticating. request.user is at that moment the AnonymousUser:

if user is not None:
    if user.is_teacher:  #            
  • Related