‘patch’ 方法在收到 URL 中的 pk 时生成 500 错误。

huangapple go评论90阅读模式
英文:

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:

  1. Request headers: {'Authorization': 'Token 13f2f18ea582e9c585c817ba52358b5b19e696a8', 'Content-Type': 'application/json', 'Content-Length': '0'}
  2. Request method: PATCH
  3. 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

  1. http_method_names = ['patch']
  2. authentication_classes = [TokenAuthentication]
  3. permission_classes = [permissions.IsAuthenticated, IsObjectOwner]
  4. queryset = Task.objects.all()
  5. lookup_field = 'pk'
  6. serializer_class = TaskSerializer
  7. def patch(self, request,):
  8. instance = self.get_object()
  9. instance.done = not instance.done
  10. instance.save()
  11. serializer = self.get_serializer(instance)
  12. return Response(serializer.data)

And this is the error I'm getting:

  1. gtd-api-1 | Traceback (most recent call last):
  2. gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/exception.py", line 47, in inner
  3. gtd-api-1 | response = get_response(request)
  4. gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py", line 181, in _get_response
  5. gtd-api-1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
  6. gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
  7. gtd-api-1 | return view_func(*args, **kwargs)
  8. gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/django/views/generic/base.py", line 70, in view
  9. gtd-api-1 | return self.dispatch(request, *args, **kwargs)
  10. gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/rest_framework/views.py", line 509, in dispatch
  11. gtd-api-1 | response = self handle_exception(exc)
  12. gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/rest_framework/views.py", line 469, in handle_exception
  13. gtd-api-1 | self raise_uncaught_exception(exc)
  14. gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
  15. gtd-api-1 | raise exc
  16. gtd-api-1 | File "/usr/local/lib/python3.8/dist-packages/rest_framework/views.py", line 506, in dispatch
  17. gtd-api-1 | response = handler(request, *args, **kwargs)
  18. gtd-api-1 | TypeError: patch() got an unexpected keyword argument 'pk'
  19. gtd-api-1 | "PATCH /api/task/toggle-done/1/ HTTP/1.1" 500 78560
  20. gtd-api-1 | Internal Server Error: /api/task/toggle-done/1/
  21. 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 &#39;pk&#39;

These are the request parameters I'm sending:

  1. Request URL: http://localhost:8000/api/task/toggle-done/1/
  2. Request headers: {&#39;Authorization&#39;: &#39;Token 13f2f18ea582e9c585c817ba52358b5b19e696a8&#39;, &#39;Content-Type&#39;: &#39;application/json&#39;, &#39;Content-Length&#39;: &#39;0&#39;}
  3. Request method: PATCH
  4. Request body: None

This is the URL Patterns:

path(&#39;task/toggle-done/&lt;int:pk&gt;/&#39;, TaskToggleDoneAPIView.as_view(), name=&#39;task_toggle_done&#39;),

This is the TaskToggleDoneAPIView implementation

  1. class TaskToggleDoneAPIView(generics.UpdateAPIView):
  2. http_method_names = [&#39;patch&#39;]
  3. authentication_classes = [TokenAuthentication]
  4. permission_classes = [permissions.IsAuthenticated,IsObjectOwner]
  5. queryset = Task.objects.all()
  6. lookup_field = &#39;pk&#39;
  7. serializer_class = TaskSerializer
  8. def patch(self, request,):
  9. instance = self.get_object()
  10. instance.done = not instance.done
  11. instance.save()
  12. serializer = self.get_serializer(instance)
  13. return Response(serializer.data)

And this is the error I'm getting:

  1. gtd-api-1 | Internal Server Error: /api/task/toggle-done/1/
  2. gtd-api-1 | Traceback (most recent call last):
  3. gtd-api-1 | File &quot;/usr/local/lib/python3.8/dist-packages/django/core/handlers/exception.py&quot;, line 47, in inner
  4. gtd-api-1 | response = get_response(request)
  5. gtd-api-1 | File &quot;/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py&quot;, line 181, in _get_response
  6. gtd-api-1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
  7. gtd-api-1 | File &quot;/usr/local/lib/python3.8/dist-packages/django/views/decorators/csrf.py&quot;, line 54, in wrapped_view
  8. gtd-api-1 | return view_func(*args, **kwargs)
  9. gtd-api-1 | File &quot;/usr/local/lib/python3.8/dist-packages/django/views/generic/base.py&quot;, line 70, in view
  10. gtd-api-1 | return self.dispatch(request, *args, **kwargs)
  11. gtd-api-1 | File &quot;/usr/local/lib/python3.8/dist-packages/rest_framework/views.py&quot;, line 509, in dispatch
  12. gtd-api-1 | response = self.handle_exception(exc)
  13. gtd-api-1 | File &quot;/usr/local/lib/python3.8/dist-packages/rest_framework/views.py&quot;, line 469, in handle_exception
  14. gtd-api-1 | self.raise_uncaught_exception(exc)
  15. gtd-api-1 | File &quot;/usr/local/lib/python3.8/dist-packages/rest_framework/views.py&quot;, line 480, in raise_uncaught_exception
  16. gtd-api-1 | raise exc
  17. gtd-api-1 | File &quot;/usr/local/lib/python3.8/dist-packages/rest_framework/views.py&quot;, line 506, in dispatch
  18. gtd-api-1 | response = handler(request, *args, **kwargs)
  19. gtd-api-1 | TypeError: patch() got an unexpected keyword argument &#39;pk&#39;
  20. gtd-api-1 | &quot;PATCH /api/task/toggle-done/1/ HTTP/1.1&quot; 500 78560
  21. gtd-api-1 | Internal Server Error: /api/task/toggle-done/1/
  22. gtd-api-1 | Traceback (most recent call last):

If I'm getting everything right, the lookup_field = &#39;pk&#39; should be handling it properly.

答案1

得分: 2

patch() 方法必须接收 pk 作为参数,因此:

英文:

The patch() method must receive pk as an argument so:

  1. class TaskToggleDoneAPIView(generics.UpdateAPIView):
  2. http_method_names = [&#39;patch&#39;]
  3. authentication_classes = [TokenAuthentication]
  4. permission_classes = [permissions.IsAuthenticated,IsObjectOwner]
  5. queryset = Task.objects.all()
  6. lookup_field = &#39;pk&#39;
  7. serializer_class = TaskSerializer
  8. def patch(self, request, pk):
  9. instance = self.get_object()
  10. instance.done = not instance.done
  11. instance.save()
  12. serializer = self.get_serializer(instance)
  13. return Response(serializer.data)

答案2

得分: 2

  1. def patch(self, request, pk):
英文:

Change:

  1. def patch(self, request,):

To:

  1. def patch(self, request, pk):

huangapple
  • 本文由 发表于 2023年2月19日 23:18:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定