Search code examples
djangodjango-rest-frameworkdjango-urlsdjango-filter

Define searchFilter URL in django rest framework


Currently I am working on a shop management project. The project is building with django, django-rest-framework and react. I want to filter the models data. So I have installed django-filter. I need to define the url like "http://localhost:8000/api/search-products/?category=oil&product=mustard oil". How can I do this?

My product search views is-

class SearchProduct(generics.ListAPIView):
    serializer_class = ProductSerializer
    filter_backends = [filters.SearchFilter]
    search_fields = ["category", "name"]

My url is-

path("api/search-product/?<str:category>&<str:name>", SearchProduct.as_view())

Its showing page not found error.

How can I define the appropriate url path?


Solution

  • you shouldn't define it as path parameters:

    path("api/search-product/", SearchProduct.as_view())  
    

    and you can call endpoint like this:

    .../api/search-product/?search=any_thing_you_want