How do i redirect a user to a specific page based on their login credentials using Django, HTML?

I'm trying to program my Django website to redirect users to custom HTML pages based on their login credentials. For example, my first user has the credentials - user=user1 pass=password1. I have created a separate user1.html page for him so when user1 enters his login info and presses login, he is automatically redirected to user1.html. When user2 logs in, he is redirected to user2.html, etc. How can I enable this in a basic and non-complicated way? I will just be using this for a small demo session, therefore, the code security and so on are not important in this case.

    <!DOCTYPE html>
        <title> XXXTechnologies </title>


            <input type="button" value="Back" onclick="history.back()">
            <p> User Login Form </p>
            Username: <input id="username" type="text" name="username" >
            Password: <input id="password" type="password" name="password" >
            <input type="submit" name="Login" value="Login">




from django.db import models

class customerlogin(models.Model):
    user_name = models.CharField(max_length=30)
    pass_word = models.CharField(max_length=30)


from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('loginpage.urls')),


from django.urls import path

from . import views 

urlpatterns = [
    path('',views.index, name='loginpage'),


from django.shortcuts import render
from django.http import HttpResponse
from loginpage.models import customerlogin

def userview(request):
    if request.customerlogin.username == 'user1':
        return render(request, 'user1slug.html', {})
    elif request.cuatomerlogin.username == 'user2':
        return render(request, 'user2slug.html', {})
        return render(request, 'default.html', {})

def index(request):
    return render(request, 'login.html')

2 answers

  • answered 2019-11-14 04:21 bmons

    Dynamic redirection, you can do it in your login view, as follows.

    def login(request):
        if request.method == "POST":
            username = request.POST['username']
            password = request.POST['password']
            user = auth.authenticate(username=username, password=password)
            if user is not  None:
                return redirect('redirect_page')
                return redirect('login')
            return render(request, 'account/login.html')

    In the redirect page you can get dynamic data from the backend. For example, if it is a profile page, you can filter the view to request.user and can show the content to logged in user.

  • answered 2019-11-14 04:25 Mukul Kumar

    In your, you can write your code like:

    def userview(request):
        if request.user.username == 'user1':
            return render(request, 'user1.html', {})
        elif request.user.username == 'user2':
            return render(request, 'user2.html', {})
            return render(request, 'default.html', {})