Redirecting a user to a specific address after logging in to Django

The problem is as follows: after the user has entered his data into the form on the page /accounts/login, upon successful login, he should be directed to the page corresponding to his username ``/username`'. At the same time, this redirection is carried out to the admin panel and each admin will have their own models displayed. I don't understand how to steal the name of the current user, and then allow only this user to enter the admin panel corresponding to his name. For example, the user entered the name ntz and some password, after that he should get to the /ntz/ - admin page of this user. There is no way other users can get there. One user - one admin panel. Here's what's in the code:

#urls.py
from django.urls import path, include
from .views import main, CustomLoginView
from PKM.admin import pkm_admin_site
from NTZ.admin import ntz_admin_site

urlpatterns=[
    path('', main, name='main'),
    path('ntz/',ntz_admin_site.urls),
    path('pkm/', pkm_admin_site.urls),
    path('accounts/', include('django.contrib.auth.urls')),
    path('accounts/login/', CustomLoginView.as_view(), name='login')
]
#login.html
{% load static %}
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}Log in to your personal account{% endblock %}</title>
</head>
<body>
    <div class="login-container">
        <h2>Login to your personal account</h2>
<form method="post">
            {{ form.as_p }}
            {% csrf_token %}
            <p><input type="submit" value="Input"></p>
        </form>
    </div>
</body>
</html>
#views.py
from django.shortcuts import render, redirect
from django.contrib.auth import login, authenticate

# Create your views here.
def main(request):
    return render(request, 'default.html')


# views.py

from django.contrib.auth.views import LoginView
from django.shortcuts import redirect


class CustomLoginView(LoginView):
    def form_valid(self, form):

        user = form.get_user()

        if self.request.user.username == 'pkm':
            return redirect('/pkm/')
        elif self.request.user.get_username() == 'ntz':
            return redirect('/ntz/')
        else:
            return super().form_valid(form)

I changed the view from class to functional, but nothing worked(

After login, the redirect URL is managed in the get_success_url method.

https://github.com/django/django/blob/main/django/contrib/auth/views.py#L40

class CustomLoginView(LoginView):
    #def form_valid(self, form):
     #   user = form.get_user()
      #  if self.request.user.username == 'pkm':
      #      return redirect('/pkm/')
      #  elif self.request.user.get_username() == 'ntz':
      #      return redirect('/ntz/')
      #  else:
      #      return super().form_valid(form)
  def get_success_url(self):
        if self.request.user.username == 'pkm':
            return redirect('/pkm/')
        elif self.request.user.get_username() == 'ntz':
            return redirect('/ntz/')
        else:
            return super().get_success_url()

Вернуться на верх