测试密码更改功能时出现问题。谢谢收看!
Feed视图正常工作
git:https://github.com/hyunsokstar/hyun4/blob/master/nomadgram/nomadgram/images/views.py
所以 request.user = allieus(登录用户)
class Feed(APIView):
def get(self, request, format=None):
user = request.user
following_users = user.following.all()
print("following_users , ", following_users)
image_list = []
for following_user in following_users:
user_images = following_user.images.all()[:2]
for image in user_images:
print("image : ", image)
image_list.append(image)
# 본인 이미지도 추가
my_images = user.images.all()[:2]
for image in my_images:
image_list.append(image)
print("image_list : ", image_list)
sorted_list = sorted(image_list, key=get_key, reverse=True)
serializer = serializers.ImageSerializer(sorted_list, many=True)
# return Response(status=200)
return Response(serializer.data)
def get_key(image):
return image.created_at
但是
ChangePassword的request.user始终为AnonymousUser打印 因此尝试更改密码时发生错误。
如果您知道原因以及解决方法,非常感谢!
github: https://github.com/hyunsokstar/hyun4/blob/master/nomadgram/nomadgram/users/views.py
class ChangePassword(APIView):
def put(self, request, username, format=None):
print("함수 실행 확인(ChangePassword) ")
user = request.user
print('user : ', user)
current_password = request.data.get('current_password',None)
if current_password is not None:
# request로 넘어온 비밀번호와 db의 비밀번호를 비교
passwords_match = user.check_password(current_password)
# 비밀번호가 정확할 경우 새로운 비밀번호를 request로부터 가져와서 user 객체에 save
if passwords_match:
new_password = request.data.get('new_password',None)
if new_password is not None:
user.set_password(new_password)
user.save()
return Response(status=status.HTTP_200_OK)
# None일 경우 400 응답
else:
return Response(status=status.HTTP_400_BAD_REQUEST)
# false일 경우 400 응답
else:
return Response(status=status.HTTP_400_BAD_REQUEST)
# None일 경우 400 응답
else:
return Response(status=status.HTTP_400_BAD_REQUEST)
答案 0 :(得分:0)
您可以使用用户名来获取用户,即
from django.contrib.auth.models import User
class ChangePassword(APIView):
def put(self, request, username, format=None):
user = User.objects.get(username=username) # This will return user