我views.py:Django的REST框架:不確定錯誤消息
class user_password(generics.UpdateAPIView):
authentication_classes = ([JSONWebTokenAuthentication])
serializer_class = user_password_serializer
def get_queryset(self):
return User.objects.get(id=self.request.user.id)
但運行它時,我得到這個:
AssertionError: Expected view user_password to be called with a URL keyword argument named "pk". Fix your URL conf, or set the
.lookup_field
attribute on the view correctly.
我知道串行器是好的,因爲當我對同一事物使用不同類型的視圖。它的工作原理:
class user_password(APIView):
authentication_classes = ([JSONWebTokenAuthentication])
def put(self, request, format=None):
serializer = user_password_serializer(data=request.data)
if serializer.is_valid():
if request.user.check_password(serializer.validated_data[
'old_password']):
request.user.set_password(serializer.validated_data[
'new_password'])
request.user.save()
return Response({'success': True,
'result': serializer.validated_data},
status=status.HTTP_200_OK)
else:
return Response({'success': False,
'result': "credential mismatch"},
status=status.HTTP_401_UNAUTHORIZED)
return Response({'success': False,
'result': serializer.errors},
status=status.HTTP_400_BAD_REQUEST)
我不想改變端點的構建方式。我確實有一個JWT認證的呼叫,並且我希望/用戶/密碼能夠簡單地將「舊密碼」和「新密碼」放入同一個用戶。
我在做什麼錯在我的generics.UpdateAPIView
類?那是什麼.lookup_field
它在說什麼?
你可以分享視圖的'urls.py'嗎? –
'url(r'^ user/password/$',views.user_password.as_view())',我真的不想在我的URL中有一個'/ {user_id} /'。我的應用程序基於JWT,用戶已通過JWT令牌識別。我只想讓端點更改用戶密碼,即匹配JWT令牌的相同用戶。 – JasonGenX