Saloss
Saloss

Reputation: 41

Doesn't see my path in urls.py, page not found

I go to "http://127.0.0.1:8000/api/v1/personalities/?fio=ti" and see "Page not found". And the same page shows this path:

api/v1/ personalities/ (?P<fio>.+)/$

Why doesn't it work?

my main urls.py:

path('personalities/', include('api.v1.personalities.urls'))

personalities.urls.py:

re_path('(?P<fio>.+)/$', PersonalitiesFilterView.as_view())

I followed the documentation from the django_filters website https://www.django-rest-framework.org/api-guide/filtering/

Upvotes: 1

Views: 53

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

The part after the ? is the query string [wiki], and not part of the path. You thus can not capture this with a regex, or path(…).

You can read the querystring as a dictionary-like structure with the request.query_params [DRF-doc] (only for APIViews) or request.GET [Django-doc].

The pattern thus looks like:

path('', PersonalitiesFilterView.as_view())

and in the view, you thus can access the value for fio with:

class PersonalitiesFilterView(ListAPIView):

    def get_queryset(self, *args, **kwargs):
        qs = super().get_queryset(*args, **kwargs)
        if 'fio' in self.request.query_params:
            value = self.request.query_params['fio']
            # do something …
        # …

Upvotes: 1

Related Questions