【问题标题】:TypeError: delete() missing 1 required positional argument: 'pk' django类型错误:delete() 缺少 1 个必需的位置参数:'pk' django
【发布时间】:2018-05-07 12:47:08
【问题描述】:

我的 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())
   ]

和项目的 urls.py 我有以下配置

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

但在http://127.0.0.1:8000/deployedcontracts/0xe9114368611e9f20cf46b76aa33319fc0ce0b585/ 提出删除请求时,我收到以下错误

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

【问题讨论】:

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


    【解决方案1】:

    您忘记在 url 模式中添加正则表达式:

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

    【讨论】:

    • 我尝试按照您的建议添加正则表达式,但没有运气。仍然得到相同的响应。
    猜你喜欢
    • 2019-11-12
    • 1970-01-01
    • 2022-01-22
    • 2021-09-12
    • 1970-01-01
    • 2022-01-11
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多