Search code examples
djangodjango-registrationdjango-logindjango-custom-user

how to redirect different types of pages to different users after login in django


I have created two different types of users - truck & company using Django. Here is my registration page of a user Registration Page

After registering, the data about whether the user is a truck or company will go to the database.

In my login page, only EmailID and Password are to be entered.

I would like to know how a user with a unique EmailID can redirects to a valid page based on the type of user.


Solution

  • You'll need something like that;

    def user_login(request):
        if request.method == 'POST':
            form = AuthenticationForm(data=request.POST)
            if form.is_valid():
                form.clean()
                login(request, form.user_cache)
                if form.user_cache.type == 'truck':
                    return HttpResponseRedirect('/some/where')
                elif form.user_cache.type == 'company':
                    return HttpResponseRedirect('/some/where/else')
        else:
            form = AuthenticationForm()
    
        return render(request, 'login.html', {'form' : form})