gpt4 book ai didi

python - Django - 登录 View : 'AnonymousUser' object has no attribute '_meta'

转载 作者:太空宇宙 更新时间:2023-11-03 20:47:42 25 4
gpt4 key购买 nike

我想使用 Django 的内置 LoginView 。它工作正常,直到我使用正确的密码登录用户(如果密码错误,它就可以工作)。然后我收到此错误:

'AnonymousUser' object has no attribute '_meta'

我已经添加了AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend']到我的settings.py,我查看了 Django login AttributeError: 'AnonymousUser' object has no attribute '_meta'以及其他网页,但他们无法帮助我。

MIDDLEWARE = [
# Must have middlewares
'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',
'simple_history.middleware.HistoryRequestMiddleware',
'admin_reorder.middleware.ModelAdminReorder',

'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',

# Custom middlewares
'utils.Middleware.RangesMiddleware.RangesMiddleware',
'utils.Middleware.AdminAccessMiddleware.AdminAccessMiddleware',
'apps.log_request.middlewares.RequestLogMiddleware',
]

.

Internal Server Error: /auth/anmelden
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Program Files\Python37\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Program Files\Python37\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Program Files\Python37\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Program Files\Python37\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Program Files\Python37\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Program Files\Python37\lib\site-packages\django\views\decorators\debug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "C:\Program Files\Python37\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Program Files\Python37\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Program Files\Python37\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Program Files\Python37\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Program Files\Python37\lib\site-packages\django\contrib\auth\views.py", line 61, in dispatch
return super().dispatch(request, *args, **kwargs)
File "C:\Program Files\Python37\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "C:\Program Files\Python37\lib\site-packages\django\views\generic\edit.py", line 142, in post
return self.form_valid(form)
File "C:\Program Files\Python37\lib\site-packages\django\contrib\auth\views.py", line 90, in form_valid
auth_login(self.request, form.get_user())
File "C:\Program Files\Python37\lib\site-packages\django\contrib\auth\__init__.py", line 126, in login
request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
File "C:\Program Files\Python37\lib\site-packages\django\utils\functional.py", line 257, in inner
return func(self._wrapped, *args)
AttributeError: 'AnonymousUser' object has no attribute '_meta'
[05/Jun/2019 21:25:28] "POST /auth/anmelden HTTP/1.1" 500 159900

此错误发生在 \django\utils\decorators.py在第 142 行( response = view_func(request, *args, **kwargs) )(我使用 PyCharm 进行调试)。我的middleware是:<django.middleware.csrf.CsrfViewMiddleware object at 0x000002105D119470>和我的view_funcfunctools.partial(<bound method LoginView.dispatch of <django.contrib.auth.views.LoginView object at 0x000002105F768C50>>) .

我的 urls.py 中的路径:

path('anmelden', LoginView.as_view(
template_name="authenticate/login.html",
authentication_form=SignInForm,
extra_context={
"selected": "authenticate",
"title": title_suffix("Anmelden", Config.verbose_name),
},
), name="login"),

我的clean在我的SignInForm中起作用:

def clean(self):
User.remove_expired_user()

cleaned_data = self.cleaned_data # That's the error. I had to use self.clean()!!!!
username = cleaned_data.get("username")
password = cleaned_data.get("password")

if "@" in username:
try:
username = User.objects.get(email__iexact=username).username
except ObjectDoesNotExist:
raise forms.ValidationError("Diese E-Mail-Adresse wurde nicht gefunden")
else:
try:
username = User.objects.get(username=username).username
except ObjectDoesNotExist:
raise forms.ValidationError("Dieser Benutzername wurde nicht gefunden")

user = authenticate(username=username, password=password)

if user is None:
try:
us = User.objects.get(username=username)
except ObjectDoesNotExist:
pass
else:
if us.check_password(password) and not us.is_active:
raise forms.ValidationError("Dieser Account ist deaktiviert. Du kannst ihn über deine E-Mail aktivieren.")
raise forms.ValidationError("Falsches Passwort")
return cleaned_data

我发现错误:

在我的clean我使用的功能self.cleaned_data而不是self.clean() .

最佳答案

在您的计算机上找到加载 Django 的目录。

python -c "import sys; print([p for p in sys.path if 'site-packages' in p])"

打开该目录并找到 django 。插入 import pdb; pdb.set_trace()https://github.com/django/django/blob/a8e2a9bac6e548d6ab2e13af6171d2fdd3b8055b/django/contrib/auth/init.py#L72并逐步检查沿途的每一步。在此过程中的某个地方,您应该达到一个期望 User 模型但获得 AnonymousUser 模型的程度。一旦找到了这一点,就会更容易找出其背后的“原因”。

关于python - Django - 登录 View : 'AnonymousUser' object has no attribute '_meta' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56465686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com