У меня есть приложение в Django 1.11, и моя проблема - ошибка:
'AnonymousUser' object has no attribute '_meta'
Появляется в момент входа в систему. Ранее пользователь был создан с помощью пользовательской формы регистрации (я используюemail
адрес вместо username
).Пользователь создан, потому что я могу войти в панель администратора по адресу /admin/
.
Ниже приведена форма входа в систему:
class LoginForm(AuthenticationForm):
email = forms.CharField(label='Email',
max_length=50,
widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Email',
'type': 'text', 'id': 'email'}))
password = forms.CharField(label='Password',
max_length=50,
widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Password',
'type': 'password', 'id': 'password'}))
field_order = ['email', 'password']
def __init__(self, *args, **kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
self.fields.pop('username')
Traceback
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/accounts/login/
Django Version: 1.11.16
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'base',
'accounts',
'django.forms',
'search',
'django_elasticsearch_dsl',
'widget_tweaks',
'ckeditor',
'ckeditor_uploader',
'raven.contrib.django.raven_compat',
'debug_toolbar']
Installed Middleware:
['raven.contrib.django.middleware.DjangoRestFrameworkCompatMiddleware',
'raven.contrib.django.middleware.SentryMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware']
Traceback:
File "project/venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "project/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "project/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "project/venv/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "project/venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)
File "project/venv/lib/python3.5/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
76. return view(request, *args, **kwargs)
File "project/venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)
File "project/venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)
File "project/venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "project/venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)
File "project/venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)
File "project/venv/lib/python3.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "project/venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)
File "project/venv/lib/python3.5/site-packages/django/contrib/auth/views.py" in dispatch
90. return super(LoginView, self).dispatch(request, *args, **kwargs)
File "project/venv/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "project/venv/lib/python3.5/site-packages/django/views/generic/edit.py" in post
183. return self.form_valid(form)
File "project/venv/lib/python3.5/site-packages/django/contrib/auth/views.py" in form_valid
119. auth_login(self.request, form.get_user())
File "project/venv/lib/python3.5/site-packages/django/contrib/auth/__init__.py" in login
155. request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
File "project/venv/lib/python3.5/site-packages/django/utils/functional.py" in inner
239. return func(self._wrapped, *args)
Exception Type: AttributeError at /accounts/login/
Exception Value: 'AnonymousUser' object has no attribute '_meta'