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

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

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 &#39;pk&#39;

These are the request parameters I'm sending:

Request URL: http://localhost:8000/api/task/toggle-done/1/
Request headers: {&#39;Authorization&#39;: &#39;Token 13f2f18ea582e9c585c817ba52358b5b19e696a8&#39;, &#39;Content-Type&#39;: &#39;application/json&#39;, &#39;Content-Length&#39;: &#39;0&#39;}
Request method: PATCH
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

class TaskToggleDoneAPIView(generics.UpdateAPIView):
    http_method_names = [&#39;patch&#39;]
    authentication_classes = [TokenAuthentication]
    permission_classes = [permissions.IsAuthenticated,IsObjectOwner]
    queryset = Task.objects.all()
    lookup_field = &#39;pk&#39;
    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 &quot;/usr/local/lib/python3.8/dist-packages/django/core/handlers/exception.py&quot;, line 47, in inner
gtd-api-1    |     response = get_response(request)
gtd-api-1    |   File &quot;/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py&quot;, line 181, in _get_response
gtd-api-1    |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
gtd-api-1    |   File &quot;/usr/local/lib/python3.8/dist-packages/django/views/decorators/csrf.py&quot;, line 54, in wrapped_view
gtd-api-1    |     return view_func(*args, **kwargs)
gtd-api-1    |   File &quot;/usr/local/lib/python3.8/dist-packages/django/views/generic/base.py&quot;, line 70, in view
gtd-api-1    |     return self.dispatch(request, *args, **kwargs)
gtd-api-1    |   File &quot;/usr/local/lib/python3.8/dist-packages/rest_framework/views.py&quot;, line 509, in dispatch
gtd-api-1    |     response = self.handle_exception(exc)
gtd-api-1    |   File &quot;/usr/local/lib/python3.8/dist-packages/rest_framework/views.py&quot;, line 469, in handle_exception
gtd-api-1    |     self.raise_uncaught_exception(exc)
gtd-api-1    |   File &quot;/usr/local/lib/python3.8/dist-packages/rest_framework/views.py&quot;, line 480, in raise_uncaught_exception
gtd-api-1    |     raise exc
gtd-api-1    |   File &quot;/usr/local/lib/python3.8/dist-packages/rest_framework/views.py&quot;, line 506, in dispatch
gtd-api-1    |     response = handler(request, *args, **kwargs)
gtd-api-1    | TypeError: patch() got an unexpected keyword argument &#39;pk&#39;
gtd-api-1    | &quot;PATCH /api/task/toggle-done/1/ HTTP/1.1&quot; 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 = &#39;pk&#39; 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 = [&#39;patch&#39;]
    authentication_classes = [TokenAuthentication]
    permission_classes = [permissions.IsAuthenticated,IsObjectOwner]
    queryset = Task.objects.all()
    lookup_field = &#39;pk&#39;
    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):

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

发表评论

匿名网友

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

确定