为了账号安全,请及时绑定邮箱和手机立即绑定

类型错误:detail() 缺少 1 个必需的位置参数:“请求”

类型错误:detail() 缺少 1 个必需的位置参数:“请求”

犯罪嫌疑人X 2022-07-12 16:20:20
我是 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"),


查看完整回答
反对 回复 2022-07-12
  • 1 回答
  • 0 关注
  • 306 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信