我在网址中包含以下内容:
path('api/delete/<int:pk>/', views.Person.as_view())
并且我正在与邮递员进行测试,这是我的新手。在查询参数中,输入
pk as the key and 4 as the value
已选择删除并...:8000 / api / delete /?pk = 4 /
我收到404 HTTP错误,提示未找到:/ api / delete /
这是我的删除功能。
def delete(self, request, pk):
print('inside delete')
person = self.get_object(pk)
print(person)
person.delete()
person.save()
return Response(status = status.HTTP_204_NO_CONTENT)
它从不输入删除内容,这表明我的路径存在问题。
谢谢
踪迹:
Internal Server Error: /api/deletestory/2/
Traceback (most recent call last):
File "/Users/John/Desktop/proj/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/John/Desktop/proj/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/John/Desktop/proj/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/John/Desktop/proj/venv/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/John/Desktop/proj/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/John/Desktop/proj/venv/lib/python3.7/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/Users/John/Desktop/proj/venv/lib/python3.7/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/John/Desktop/proj/venv/lib/python3.7/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
TypeError: delete() got multiple values for argument 'pk'
[10/Mar/2019 07:30:54] "DELETE /api/delete/2/ HTTP/1.1" 500 14281
视图:
class Person(APIView):
def get_object(self, pk):
try:
print('inside get object')
return Person.objects.get(pk = pk)
except Person.DoesNotExist:
raise Http404
def get(self, request, pk):
print('inside get')
person = self.get_object(pk)
serializer = PersonSerializer(person)
return Response(serializer.data)
def delete(self, pk):
print('inside delete')
person = self.get_object(pk)
print(person)
person.delete()
return Response(status = status.HTTP_204_NO_CONTENT)