Error 'AnonymousUser' object has no attribute '_meta'

I am writing an authorization system on my site and when I try to log in, I get the error 'AnonymousUser' object has no attribute '_meta'. Why is this happening? Here is structure of my project:

├───ithogwarts
│   │   db.sqlite3
│   │   manage.py
│   │
│   ├───ithogwarts
│   │   │   asgi.py
│   │   │   settings.py
│   │   │   urls.py
│   │   │   wsgi.py
│   │   │   __init__.py
│   │   │
│   │   └───__pycache__
│   │           settings.cpython-310.pyc
│   │           settings.cpython-39.pyc
│   │           urls.cpython-310.pyc
│   │           urls.cpython-39.pyc
│   │           wsgi.cpython-310.pyc
│   │           wsgi.cpython-39.pyc
│   │           __init__.cpython-310.pyc
│   │           __init__.cpython-39.pyc
│   │
│   ├───main
│   │   │   admin.py
│   │   │   apps.py
│   │   │   models.py
│   │   │   tests.py
│   │   │   urls.py
│   │   │   views.py
│   │   │   __init__.py
│   │   │
│   │   ├───migrations
│   │   │   │   __init__.py
│   │   │   │
│   │   │   └───__pycache__
│   │   │           __init__.cpython-310.pyc
│   │   │           __init__.cpython-39.pyc
│   │   │
│   │   ├───static
│   │   │   └───main
│   │   │       ├───css
│   │   │       │       footer.css
│   │   │       │       header.css
│   │   │       │       index.css
│   │   │       │
│   │   │       ├───img
│   │   │       │       1.jpg
│   │   │       │       2.jpg
│   │   │       │       3.jpg
│   │   │       │       4.jpg
│   │   │       │       castle1.png
│   │   │       │       cloud.png
│   │   │       │       competition.png
│   │   │       │       damb.png
│   │   │       │       faculties.png
│   │   │       │       leo.png
│   │   │       │       logo-icon.ico
│   │   │       │       logo.png
│   │   │       │       magic_levels.png
│   │   │       │       magic_wand.png
│   │   │       │       master-classes.png
│   │   │       │       news.png
│   │   │       │
│   │   │       └───js
│   │   │               script.js
│   │   │
│   │   ├───templates
│   │   │   └───main
│   │   │           index.html
│   │   │           layout.html
│   │   │           level_magic.html
│   │   │
│   │   └───__pycache__
│   │           admin.cpython-310.pyc
│   │           admin.cpython-39.pyc
│   │           apps.cpython-310.pyc
│   │           apps.cpython-39.pyc
│   │           models.cpython-310.pyc
│   │           models.cpython-39.pyc
│   │           urls.cpython-310.pyc
│   │           urls.cpython-39.pyc
│   │           views.cpython-310.pyc
│   │           views.cpython-39.pyc
│   │           __init__.cpython-310.pyc
│   │           __init__.cpython-39.pyc
│   │
│   ├───templates
│   │   └───registration
│   └───users
│       │   admin.py
│       │   apps.py
│       │   forms.py
│       │   models.py
│       │   tests.py
│       │   urls.py
│       │   views.py
│       │   __init__.py
│       │
│       ├───migrations
│       │   │   __init__.py
│       │   │
│       │   └───__pycache__
│       │           __init__.cpython-310.pyc
│       │           __init__.cpython-39.pyc
│       │
│       ├───static
│       │   └───users
│       │       └───css
│       │               login.css
│       │               register.css
│       │
│_______├───templates
            └───users
                    login.html
                    register.html

users/urls.py:

from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
from . import views

urlpatterns = [
    path('login', views.LoginUser.as_view(), name="login"),
    path('logout', views.logout_user, name="logout"),
    path('register', views.RegisterUser.as_view(), name="register")
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

forms.py:

    from django import forms
    from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
    from django.contrib.auth.models import User
    
    
    class RegisterUserForm(UserCreationForm):
        username = forms.CharField(label="Имя", widget=forms.TextInput(attrs={'class': 'register__form-title form-control',
                                                                              'placeholder': 'введите ваше имя'}))
        email = forms.CharField(label="Почта", widget=forms.TextInput(attrs={'class': 'register__form-title form-control',
                                                                              'placeholder': 'введите вашу почту'}))
        password1 = forms.CharField(label="Пароль", widget=forms.TextInput(attrs={'class': 'register__form-title form-control',
                                                                              'placeholder': 'введите пароль'}))
        password2 = forms.CharField(label="Подтверждение пароля", widget=forms.TextInput(attrs={'class': 'register__form-title form-control',
                                                                              'placeholder': 'подтвердите ваш пароль'}))
    
        def __init__(self, *args, **kwargs):
            super(UserCreationForm, self).__init__(*args, **kwargs)
            for field_name in ['username', 'password1', 'password2']:
                self.fields[field_name].help_text = None
    
        class Meta:
            model = User
            fields = ('username', 'email', 'password1', 'password2')
            widgets = {
                'username': forms.TextInput(attrs={'class': 'form-input'}),
                'email': forms.EmailInput(attrs={'class': 'form-control'}),
                'password1': forms.PasswordInput(attrs={'class': 'form-input'}),
                'password2': forms.PasswordInput(attrs={'class': 'form-input'})
            }
    
    class LoginUserForm(AuthenticationForm):
        email = forms.CharField(label="Почта", widget=forms.TextInput(attrs={'class': 'register__form-title form-control',
                                                                              'placeholder': 'введите вашу почту'}))
        password = forms.CharField(label="Пароль", widget=forms.PasswordInput(attrs={'class': 'register__form-title form-control',
                                                                              'placeholder': 'введите пароль'}))
    
        def __init__(self, *args, **kwargs):
            super(LoginUserForm, self).__init__(*args, **kwargs)
            self.fields.pop('username')
    
        class Meta:
            model = User
            fields = ('email', 'password')
users/views.py:
from django.contrib.auth import logout, login
from django.contrib.auth.views import LoginView
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.urls import reverse_lazy
from django.views import generic

from .forms import RegisterUserForm, LoginUserForm


def logout_user(request):
    logout(request)
    return redirect('home')


class RegisterUser(generic.CreateView):
    form_class = RegisterUserForm
    success_url = reverse_lazy('login')
    template_name = 'users/register.html'


class LoginUser(LoginView):
    form_class = LoginUserForm
    template_name = 'users/login.html'

Error:

AttributeError at /users/login
'AnonymousUser' object has no attribute '_meta'
Request Method: POST
Request URL:    http://127.0.0.1:8000/users/login
Django Version: 3.2.9
Exception Type: AttributeError
Exception Value:    
'AnonymousUser' object has no attribute '_meta'
Exception Location: C:\Users\belog\ithogwarts\venv\lib\site-packages\django\utils\functional.py, line 247, in inner
Python Executable:  C:\Users\belog\ithogwarts\venv\Scripts\python.exe
Python Version: 3.10.0
Python Path:    
['C:\\Users\\belog\\ithogwarts\\ithogwarts',
 'C:\\Users\\belog\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip',
 'C:\\Users\\belog\\AppData\\Local\\Programs\\Python\\Python310\\DLLs',
 'C:\\Users\\belog\\AppData\\Local\\Programs\\Python\\Python310\\lib',
 'C:\\Users\\belog\\AppData\\Local\\Programs\\Python\\Python310',
 'C:\\Users\\belog\\ithogwarts\\venv',
 'C:\\Users\\belog\\ithogwarts\\venv\\lib\\site-packages']
Server time:    Sat, 11 Dec 2021 05:38:27 +0000
Traceback Switch to copy-and-paste view
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\core\handlers\exception.py, line 47, in inner
                response = get_response(request) …
▶ Local vars
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\core\handlers\base.py, line 181, in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\views\generic\base.py, line 70, in view
            return self.dispatch(request, *args, **kwargs) …
▶ Local vars
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\utils\decorators.py, line 43, in _wrapper
        return bound_method(*args, **kwargs) …
▶ Local vars
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\views\decorators\debug.py, line 89, in sensitive_post_parameters_wrapper
            return view(request, *args, **kwargs) …
▶ Local vars
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\utils\decorators.py, line 43, in _wrapper
        return bound_method(*args, **kwargs) …
▶ Local vars
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\utils\decorators.py, line 130, in _wrapped_view
                    response = view_func(request, *args, **kwargs) …
▶ Local vars
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\utils\decorators.py, line 43, in _wrapper
        return bound_method(*args, **kwargs) …
▶ Local vars
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\views\decorators\cache.py, line 44, in _wrapped_view_func
        response = view_func(request, *args, **kwargs) …
▶ Local vars
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\contrib\auth\views.py, line 63, in dispatch
        return super().dispatch(request, *args, **kwargs) …
▶ Local vars
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\views\generic\base.py, line 98, in dispatch
        return handler(request, *args, **kwargs) …
▶ Local vars
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\views\generic\edit.py, line 142, in post
            return self.form_valid(form) …
▶ Local vars
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\contrib\auth\views.py, line 92, in form_valid
        auth_login(self.request, form.get_user()) …
▶ Local vars
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\contrib\auth\__init__.py, line 129, in login
    request.session[SESSION_KEY] = user._meta.pk.value_to_string(user) …
▶ Local vars
C:\Users\belog\ithogwarts\venv\lib\site-packages\django\utils\functional.py, line 247, in inner
        return func(self._wrapped, *args) …
▶ Local vars
Back to Top