gpt4 book ai didi

python - 未实现错误 : Django doesn't provide a DB representation for AnonymousUser

转载 作者:行者123 更新时间:2023-11-28 22:16:19 25 4
gpt4 key购买 nike

我收到以下错误:

Traceback:

File "C:\Users\HP\GST\lib\site-packages\django\core\handlers\exception.py"
in inner
35. response = get_response(request)

File "C:\Users\HP\GST\lib\site-packages\django\core\handlers\base.py" in
_get_response
128. response = self.process_exception_by_middleware(e,
request)

File "C:\Users\HP\GST\lib\site-packages\django\core\handlers\base.py" in
_get_response
126. response = wrapped_callback(request, *callback_args,
**callback_kwargs)

File "C:\Users\HP\Desktop\erpcloud\accounts\views.py" in change_password
31. if form.is_valid():

File "C:\Users\HP\GST\lib\site-packages\django\forms\forms.py" in is_valid
179. return self.is_bound and not self.errors

File "C:\Users\HP\GST\lib\site-packages\django\forms\forms.py" in errors
174. self.full_clean()

File "C:\Users\HP\GST\lib\site-packages\django\forms\forms.py" in
full_clean
376. self._clean_fields()

File "C:\Users\HP\GST\lib\site-packages\django\forms\forms.py" in
_clean_fields
397. value = getattr(self, 'clean_%s' % name)()

File "C:\Users\HP\GST\lib\site-packages\django\contrib\auth\forms.py" in
clean_old_password
366. if not self.user.check_password(old_password):

File "C:\Users\HP\GST\lib\site-packages\django\contrib\auth\models.py" in
check_password
396. raise NotImplementedError("Django doesn't provide a DB
representation for AnonymousUser.")

Exception Type: NotImplementedError at /accounts/change-password/
Exception Value: Django doesn't provide a DB representation for
AnonymousUser.

我的观点是这样的:

def change_password(request):
if request.method == 'POST':
form = PasswordChangeForm(data=request.POST, user=request.user)

if form.is_valid():
form.save()
update_session_auth_hash(request, user)
return redirect(reverse('company:Dashboard'))
else:
return redirect(reverse('accounts:change_password'))
else:
form = PasswordChangeForm(user=request.user)

args = {'form': form}
return render(request, 'accounts/change_password.html', args)

首先,我认为这是因为我没有更新 Django,但现在我更新了,但我收到了同样的错误。

我查看了其他用户提出的一些解决方案,但没有一个适用于我的情况

有什么帮助吗?

最佳答案

View 本身没有任何问题。问题在于,如果用户 登录,则request.user 将指向一个AnonymousUser 对象。您可以将其视为虚拟用户。然而,该用户没有数据库表示,因为我们对该用户一无所知。更多的是用来提供统一的接口(interface)。

既然 request.user 是一个 AnonymousUser,您打算更改该用户的密码,但您不能将其存储到数据库中,因此会出现错误。

因此,用户首先需要登录,然后 request.user 将成为真实用户,并且更新密码应该有效。

但是我建议在 View 中添加一个 @login_required 装饰器以防止这种情况发生:

from django.contrib.auth.decorators import login_required

<b>@login_required</b>
def change_password(request):
if request.method == 'POST':
form = PasswordChangeForm(data=request.POST, user=request.user)

if form.is_valid():
form.save()
update_session_auth_hash(request, user)
return redirect(reverse('company:Dashboard'))
else:
return redirect(reverse('accounts:change_password'))
else:
form = PasswordChangeForm(user=request.user)

args = {'form': form}
return render(request, 'accounts/change_password.html', args)

关于python - 未实现错误 : Django doesn't provide a DB representation for AnonymousUser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52273087/

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