英文:
The 'patch' method generating a 500 error when recieving a pk in the URL
问题
I'm sending a PATCH request (naturally) passing a pk and I'm getting a TypeError: patch() got an unexpected keyword argument 'pk'
.
These are the request parameters I'm sending:
Request headers: {'Authorization': 'Token 13f2f18ea582e9c585c817ba52358b5b19e696a8', 'Content-Type': 'application/json', 'Content-Length': '0'}
Request method: PATCH
Request body: None
This is the URL Patterns:
path('task/toggle-done/<int:pk>/', TaskToggleDoneAPIView.as_view(), name='task_toggle_done'),
This is the TaskToggleDoneAPIView
implementation
http_method_names = ['patch']
authentication_classes = [TokenAuthentication]
permission_classes = [permissions.IsAuthenticated, IsObjectOwner]
queryset = Task.objects.all()
lookup_field = 'pk'
serializer_class = TaskSerializer
def patch(self, request,):
instance = self.get_object()
instance.done = not instance.done
instance.save()
serializer = self.get_serializer(instance)
return Response(serializer.data)
And this is the error I'm getting:
gtd-api-1 | Traceback (most recent call last):
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/exception.py", line 47, in inner
gtd-api-1 | response = get_response(request)
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py", line 181, in _get_response
gtd-api-1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
gtd-api-1 | return view_func(*args, **kwargs)
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/django/views/generic/base.py", line 70, in view
gtd-api-1 | return self.dispatch(request, *args, **kwargs)
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/rest_framework/views.py", line 509, in dispatch
gtd-api-1 | response = self handle_exception(exc)
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/rest_framework/views.py", line 469, in handle_exception
gtd-api-1 | self raise_uncaught_exception(exc)
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
gtd-api-1 | raise exc
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/rest_framework/views.py", line 506, in dispatch
gtd-api-1 | response = handler(request, *args, **kwargs)
gtd-api-1 | TypeError: patch() got an unexpected keyword argument 'pk'
gtd-api-1 | "PATCH /api/task/toggle-done/1/ HTTP/1.1" 500 78560
gtd-api-1 | Internal Server Error: /api/task/toggle-done/1/
gtd-api-1 | Traceback (most recent call last):
If I'm getting everything right, the lookup_field = 'pk'
should be handling it properly.
英文:
I'm sending a PATCH request (naturally) passing a pk and I'm getting a TypeError: patch() got an unexpected keyword argument 'pk'
These are the request parameters I'm sending:
Request URL: http://localhost:8000/api/task/toggle-done/1/
Request headers: {'Authorization': 'Token 13f2f18ea582e9c585c817ba52358b5b19e696a8', 'Content-Type': 'application/json', 'Content-Length': '0'}
Request method: PATCH
Request body: None
This is the URL Patterns:
path('task/toggle-done/<int:pk>/', TaskToggleDoneAPIView.as_view(), name='task_toggle_done'),
This is the TaskToggleDoneAPIView
implementation
class TaskToggleDoneAPIView(generics.UpdateAPIView):
http_method_names = ['patch']
authentication_classes = [TokenAuthentication]
permission_classes = [permissions.IsAuthenticated,IsObjectOwner]
queryset = Task.objects.all()
lookup_field = 'pk'
serializer_class = TaskSerializer
def patch(self, request,):
instance = self.get_object()
instance.done = not instance.done
instance.save()
serializer = self.get_serializer(instance)
return Response(serializer.data)
And this is the error I'm getting:
gtd-api-1 | Internal Server Error: /api/task/toggle-done/1/
gtd-api-1 | Traceback (most recent call last):
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/exception.py", line 47, in inner
gtd-api-1 | response = get_response(request)
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py", line 181, in _get_response
gtd-api-1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
gtd-api-1 | return view_func(*args, **kwargs)
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/django/views/generic/base.py", line 70, in view
gtd-api-1 | return self.dispatch(request, *args, **kwargs)
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/rest_framework/views.py", line 509, in dispatch
gtd-api-1 | response = self.handle_exception(exc)
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/rest_framework/views.py", line 469, in handle_exception
gtd-api-1 | self.raise_uncaught_exception(exc)
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
gtd-api-1 | raise exc
gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/rest_framework/views.py", line 506, in dispatch
gtd-api-1 | response = handler(request, *args, **kwargs)
gtd-api-1 | TypeError: patch() got an unexpected keyword argument 'pk'
gtd-api-1 | "PATCH /api/task/toggle-done/1/ HTTP/1.1" 500 78560
gtd-api-1 | Internal Server Error: /api/task/toggle-done/1/
gtd-api-1 | Traceback (most recent call last):
If I'm getting everything right, the lookup_field = 'pk'
should be handling it properly.
答案1
得分: 2
patch()
方法必须接收 pk
作为参数,因此:
英文:
The patch()
method must receive pk
as an argument so:
class TaskToggleDoneAPIView(generics.UpdateAPIView):
http_method_names = ['patch']
authentication_classes = [TokenAuthentication]
permission_classes = [permissions.IsAuthenticated,IsObjectOwner]
queryset = Task.objects.all()
lookup_field = 'pk'
serializer_class = TaskSerializer
def patch(self, request, pk):
instance = self.get_object()
instance.done = not instance.done
instance.save()
serializer = self.get_serializer(instance)
return Response(serializer.data)
答案2
得分: 2
def patch(self, request, pk):
英文:
Change:
def patch(self, request,):
To:
def patch(self, request, pk):
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论