代码之家  ›  专栏  ›  技术社区  ›  Alfa Bravo

如果不复制API端点,Django Get request返回空数组

  •  1
  • Alfa Bravo  · 技术社区  · 6 年前

    在过去的两个月里,我一直在为自己无法确定的奇怪行为而挣扎。

    我的django app url文件之一如下所示:

    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"),
    } 
    

    path('containers/', GetProductContainers.as_view()), 在我的urlpatterns中是两次。原因是,只要我删除一个,它就会返回一个空数组。 我把哪一个移走并不重要!

    有没有人能想出一个解释,或者我如何开始调试它?

    1 回复  |  直到 6 年前
        1
  •  5
  •   Vitalii Volkov    6 年前

    我相信这是因为您创建的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']>]