1

Hi I'm having a very annoying issue with django: I've setup many urls paths and they all work fine except one and I really can't figure out why:

Urls

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'), ] 

Views

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 

Error message

enter image description here

1 Answer 1

1

?survey=…&string=… is the query string [wiki], this is not part of the path. You thus can not capture these with path(…). The path thus looks like:

path('all_foods/', FoodListCreateAPIView.as_view(), name='food-list'),

you then thus retrieve the parameters with:

survey = self.request.query_params['survey'] food = self.request.query_params['string']

It is however not guaranteed that the survey or string keys are in the querystring, you thus should check this with:

if 'survey' in request.query_params and 'string' in request.query_params: # … else: # …
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.