我是 Django 的新手。最初我有这个基于函数的视图 -@api_view(['GET', 'PUT', 'DELETE'])def detail(self,request, pk):"""Retrieve, update or delete a product instance."""try: product = latesttrial.objects.get(pk=pk) newproduct = latesttrial.objects.all()except latesttrial.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND)if request.method == 'GET': serializer = latestSerializer(product,context={'request': request}) return Response(serializer.data)elif request.method == 'PUT': serializer = latestSerializer(product, data=request.data,context={'request': request}) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)然后它给出了这个错误 -TypeError: detail() missing 1 required positional argument: 'pk' 为此,我根据此答案进行了这些更改- 缺少 1 个必需的位置参数:'pk'然后我有了这个基于函数的视图@api_view(['GET', 'PUT', 'DELETE'])def detail(request, *args, **kwargs):"""Retrieve, update or delete a product instance."""try: pk = self.kwargs.get('pk') product = latesttrial.objects.get(pk=pk) newproduct = latesttrial.objects.all()except latesttrial.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND)if request.method == 'GET': pk = self.kwargs.get('pk') serializer = latestSerializer(product,context={'request': request}) return Response(serializer.data)elif request.method == 'PUT': pk = self.kwargs.get('pk') serializer = latestSerializer(product, data=request.data,context={'request': request}) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)然后我有这个错误 -TypeError: detail() missing 1 required positional argument: 'request'
1 回答
HUX布斯
TA贡献1876条经验 获得超6个赞
您可以将更改恢复到以前的版本并进行更改:
url(r'detail',detail,name="newfunction"),
对此:
url(r'detail(?P<pk>[0-9]+)/$',detail,name="newfunction"),
添加回答
举报
0/150
提交
取消