I get the following error:
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.
My code in which is shown line 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,
})
)
Firstly, I thought that this was due to the fact that I didn't have Django updated, but now I have, and I receive the same error.
I've looked at some solutions that other users asked, but none applied in my case
Any help, please ?
if not request.user:
This if statement only checks if request.user
is a truthy object, which it will always be. If a user is not authenticated, it is an AnonymousUser
which does not have a database representation. set_password()
and various other methods will raise a NotImplementedError
.
To fix it, change the if statement to:
if not request.user.is_authenticated():
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.