TypeError:delete()缺少1个必需的位置参数:' pk' Django的

时间:2018-05-07 12:47:08

标签: python django django-rest-framework django-views

我的django应用程序中有一个视图,如下所示

class DeployedContractsList(APIView):    
    def get_Contract(self, address):
        contracts = DeployedContracts.objects.all()
        if address:
            contracts = contracts.filter(
                Q(deployed_contract_address__iexact=address)
            )
        return contracts

    def get(self, request, email, format=None):
        service_providers = self.get_queryset(email)
        serializer = DeployedContractsSerializer(service_providers, many=True)
        return Response(data=serializer.data)


    def delete(self, request, pk, format=None):
        item = self.get_Contract(address=pk)
        item.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

我已按以下方式配置我的应用网址

    router = DefaultRouter()

    urlpatterns = [
        url(r'^(?P<email>[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/$', views.DeployedContractsList.as_view()),
        url(r'^(?P<pk>)/$', views.DeployedContractsList.as_view()),
        url(r'^', views.DeployedContractsList.as_view())
   ]

和project&#39; urls.py我有以下配置

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^deployedcontracts', include('deployedcontracts.urls')),
]

但是在http://127.0.0.1:8000/deployedcontracts/0xe9114368611e9f20cf46b76aa33319fc0ce0b585/发出删除请求后,我收到了以下错误

enter image description here

尽管我在请求URL中附加了pk,即0xe9114368611e9f20cf46b76aa33319fc0ce0b585,因为它可以在我的URL中看到。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

您忘了将正则表达式添加到url patern:

url(r'^(?P<pk>[\w-]+)/$', views.DeployedContractsList.as_view()),