Django 无法解析 url 路径 404
Django can't resolve url path 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
错误信息
?survey=…&string=…
是 query string [wiki],这是路径的 而不是 部分。因此,您无法使用 path(…)
捕获这些。因此路径看起来像:
path(<b>'all_foods/'</b>, FoodListCreateAPIView.as_view(), name='food-list'),
然后您可以通过以下方式检索参数:
survey = self.request<b>.query_params['survey']</b>
food = self.request<b>.query_params['string']</b>
但是 不 保证 survey
或 string
键在查询字符串中,因此您应该检查它:
if 'survey' in request.query_params and 'string' in request.query_params:
# …
else:
# …
您好,我在使用 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
错误信息
?survey=…&string=…
是 query string [wiki],这是路径的 而不是 部分。因此,您无法使用 path(…)
捕获这些。因此路径看起来像:
path(<b>'all_foods/'</b>, FoodListCreateAPIView.as_view(), name='food-list'),
然后您可以通过以下方式检索参数:
survey = self.request<b>.query_params['survey']</b>
food = self.request<b>.query_params['string']</b>
但是 不 保证 survey
或 string
键在查询字符串中,因此您应该检查它:
if 'survey' in request.query_params and 'string' in request.query_params:
# …
else:
# …