首页 > 解决方案 > Django 无法解析 url 路径 404

问题描述

嗨,我对 django 有一个非常烦人的问题:我设置了许多 url 路径,除了一个之外它们都工作正常,我真的不知道为什么:

网址

urlpatterns = [
    # path('foods/search', food_search),
    path('food_list/',
         FoodListVersionListCreateAPIView.as_view(),
         name='foodList-list'),
    path('all_foods/<str:survey>/<str:string>',
         FoodListCreateAPIView.as_view(),
         name='food-list'),
    path('food_classification/',
         FoodClassificationListCreateAPIView.as_view(),
         name='foodClassification-list'),
    path('food/<str:survey>/<str:string>/',
         FoodDetailAPIView.as_view(),
         name='food-detail'),
]

意见

class FoodListCreateAPIView(generics.ListCreateAPIView):
    queryset = Food.objects.all()
    serializer_class = FoodSerializer
    filter_backends = [DjangoFilterBackend, filters.SearchFilter]
    filterset_fields = ['description', 'food_code',
                        'cofid_code', 'foodex_code',
                        'food_classification']
    search_fields = ['food_list', 'SYSTEM_TIME']
    permission_classes = [permissions.IsAuthenticatedOrReadOnly]

    def get_queryset(self):
        assert self.queryset is not None, (
            "'%s' should either include a `queryset` attribute, "
            "or override the `get_queryset()` method."
            % self.__class__.__name__
        )

        survey = self.request.query_params['survey']
        food = self.request.query_params['string']
        raw_queryset = perform_food_search(survey, food)
        queryset = Food.objects.filter(pk__in=[i.pk for i in raw_queryset])

        if isinstance(queryset, QuerySet):
            # Ensure queryset is re-evaluated on each request.
            queryset = queryset.all()
        return queryset

错误信息

在此处输入图像描述

标签: djangourldjango-rest-frameworkhttprequest

解决方案



推荐阅读