A
A
Andrei1penguin12020-11-03 02:34:14
Django
Andrei1penguin1, 2020-11-03 02:34:14

How to fix invalid request.user output in django?

Good day, when creating authorization, I abandoned the LoginView bicycle, since redefining and remaking it for what I need will take much more time than if I write it myself
To the essence of the problem:
Here are some fields of the model

class User(AbstractUser):
    username = models.CharField(max_length=100, null=True, blank=True, unique=True)
    login = models.CharField(max_length=100, verbose_name="Логин", blank=False, default="NULL")
    password = models.CharField(max_length=100, verbose_name="Пароль", blank=False, default="Null")

A piece of code from the authorization view:
from django.views.generic import View
from django.contrib import auth
from myapp.models import User


class Login(View):
    def post(self, request):
        login = request.POST.get("username", "")
        password = request.POST.get("password", "")
        login_objects = [login.get("login") for login in User.objects.all().values()]
        if login in login_objects:
            if User.objects.filter(login = login).values()[0].get("password") == password:
                user = auth.authenticate(login=login, password=password)
                auth.login(request, user)
                try:
                    return render (request, "page.html")
                finally:
                    return redirect("url_page")

There is also a registered superuser

And anywhere at any time request.user issues an admin, even AnonymousUser cannot be obtained

What to do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rodion, 2020-11-03
@rodion4dev

Probably, the whole point is that you have not overridden the AUTH_USER_MODEL setting . I propose to dig in this direction, because, if my memory serves me, the user gets into the request object based on exactly that setting.

A
alternativshik, 2020-11-03
@alternativshik

some bullshit written.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question