gpt4 book ai didi

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

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

我收到以下错误:

Internal Server Error: /api/user/change-password/
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 89, in dispatch
return handler(request, *args, **kwargs)

File "/opt/project/views.py", line 227, in put
request.user.set_password(password)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py", line 480, in set_password
raise NotImplementedError("Django doesn't provide a DB representation for User.")
NotImplementedError: Django doesn't provide a DB representation for User.

我的代码显示在第 227 行:

class ChangePasswordAPI(View):
@staticmethod
def put(request):
data = request.read()
payload = json.loads(data)
password = payload['password']

if not request.user:
return HttpResponse(
json.dumps({
'success': False,
'error': 'user_not_exist'
})
)
# the following line is 227
request.user.set_password(password)
request.user.save()

return HttpResponse(
json.dumps({
'success': True,
})
)

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

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

请问有什么帮助吗?

最佳答案

if not request.user:

这个 if 语句仅检查 request.user 是否是一个真实的对象,它永远都是。如果用户未经过身份验证,则它是一个没有数据库表示形式的 AnonymousUserset_password() 和各种其他方法将引发 NotImplementedError

要修复此问题,请将 if 语句更改为:

if not request.user.is_authenticated():

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

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