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>
    <html>
    <head>
        <title> XXXTechnologies </title>

    </head>
    <body>
        <form>

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


        </form>

    </body>
    </html>

loginpage models.py

from django.db import models

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

myproject urls.py

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




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

loginpage urls.py

from django.urls import path

from . import views 

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

loginpage views.py

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', {})
    else:
        return render(request, 'default.html', {})


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

2 answers

  • answered 2019-11-14 04:21 user6837382

    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:
                auth.login(request,user)
                return redirect('redirect_page')
            else:
                return redirect('login')
        else:
            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 kumarm5

    In your views.py, 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', {})
        else:
            return render(request, 'default.html', {})