如果我没有复制API端点,Django Get请求将返回空数组

Alf*_*avo 1 django django-rest-framework

在过去的两个月里,我一直在以非常奇怪的行为挣扎,我无法确定.

我的一个django app urls文件看起来像这样:

urlpatterns = {
    path('containers/', GetProductContainers.as_view()),
    path('delete/<deleteTime>', DeleteProcessedStockTime.as_view()),
    path('containers/', GetProductContainers.as_view()),
    path('input/', InsertMultiProcessedStock.as_view()),
    path('<str:stockT>/', ProcessedStockTimeView.as_view(), name="stockstime"),
    path('', ProductListDetailsView.as_view(), name="details"),
} 
Run Code Online (Sandbox Code Playgroud)

如您所见,此路径path('containers/', GetProductContainers.as_view()),在我的urlpatterns中是两次.这样做的原因是,只要删除一个,它就会返回一个空数组.我删除哪一个都没关系!如果两者都在那里,我得到了我期望的319条记录.我可以删除两个中的任何一个,它将返回一个空数组,但只要我有2它再次工作.

任何人都可以想到对此的解释或我如何开始调试它?

小智 5

我相信这是因为您创建了urlpatterns作为集合而不是列表.集合是无序类型,因此不会以正确的顺序解析url模式.

例:

>>> {
...     path('containers/', TestView.as_view()),
...     path('delete/<deleteTime>', TestView.as_view()),
...     path('input/', TestView.as_view()),
...     path('<str:stockT>/', TestView.as_view(), name="stockstime"),
...     path('', TestView.as_view(), name="details"),
... }
{<URLPattern '<str:stockT>/' [name='stockstime']>, <URLPattern '' [name='details']>, <URLPattern 'containers/'>, <URLPattern 'delete/<deleteTime>'>, <URLPattern 'input/'>}


>>> [
...     path('containers/', TestView.as_view()),
...     path('delete/<deleteTime>', TestView.as_view()),
...     path('input/', TestView.as_view()),
...     path('<str:stockT>/', TestView.as_view(), name="stockstime"),
...     path('', TestView.as_view(), name="details"),
... ]
[<URLPattern 'containers/'>, <URLPattern 'delete/<deleteTime>'>, <URLPattern 'input/'>, <URLPattern '<str:stockT>/' [name='stockstime']>, <URLPattern '' [name='details']>]
Run Code Online (Sandbox Code Playgroud)