Checking oauth2 authorization django

I'm still new to Python django and I'm asking for your help I need to display the user's nickname,But it doesn't display. That's what I have in views

from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from collections.abc import Iterable
from index.auth import *

@login_required(login_url="/oauth2/login")
def get_authenticated_user(request):
    return JsonResponse({"msg": "hello"})

def oauth_login(request):
    return redirect(auth_url_discord)

def discord_login_redirect(request: HttpRequest, *args, **kwargs):
    code = request.GET.get('code')
    exchange_user = exchange_code(code)
    discord_user_auth = DiscordAuthenticationBackend.authenticate(request=request, user=exchange_user)
    if isinstance(discord_user_auth, Iterable):
        discord_user_auth = list(discord_user_auth).pop()
    login(request, discord_user_auth)
    return redirect(index)

def exchange_code(code: str):
    data = {
        "client_id": client_id,
        "client_secret": client_secret,
        "grant_type": "authorization_code",
        "code": code,
        "redirect_uri": redirect_uri,
        "scope": "identifiy",
    }
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    response = requests.post("https://discord.com/api/oauth2/token", data=data, headers=headers)
    credentials = response.json()
    access_token = credentials['access_token']
    response = requests.get("https://discord.com/api/v6/users/@me", headers={
    'Authorization': 'Bearer %s' % access_token
    })
    user = response.json()
    return user

How do I create a user, in auth

from django.contrib.auth.backends import BaseBackend
from django.contrib.auth.models import User
from .models import User

class DiscordAuthenticationBackend(BaseBackend):
    def authenticate(request, user) -> User:
        find_user = User.objects.filter(id=user['id'])
        if len(find_user) == 0:
            new_user = User.objects.create_new_discord_user(user)
            return new_user
        return find_user

def get_user(self, user_id):
    try:
        return User.objects.get(pk+user_id)
    except User.DoesNotExist:
        return None

How do I save a user in discordmanager

from django.contrib.auth import models

class DiscordUserOauthManager(models.UserManager):
    def create_new_discord_user(self, user):
        new_user = self.create(
                id = user['id'],
                username = user['username'],
                discriminator = user['discriminator'],
                avatar = user['avatar'],
                public_flags = user['public_flags'],
                flags = user['flags'],
                locale = user['locale'],
                mfa_enabled = user['mfa_enabled']
            )
        return new_user

I also wrote in class model user

    def is_authenticated(self, request):
        return True

And my html code

            {% if user.is_authenticated %}  
            <img class="profile__avatar" src="https://cdn.discordapp.com/avatars/{{u.id}}/{{u.avatar}}" alt="">
            <h2 class="profile__name border_bottom" style="margin:0;">
                <span data-index="8-0" data-value="User" data-translation="User" data-type="trSpan">{{u.username}}</span>
            </h2>
                <div class="border_bottom btn-sb">
                    <a class="btn0001" href="">Logout</a>
                    <a type="button" class="btn0001" href="">Profile</a>
                </div>
            {% else %}
            <div class="border_bottom btn-sb">
                <a type="button" class="btn0001" href="{% url 'oauth_login' %}">login</a>
            </div>      
            {% endif %} 

Tell me at what point I was wrong and what needs to be changed so that the verification of the authorized user is passed and the nickname is displayed

Back to Top