【发布时间】:2021-09-15 21:15:50
【问题描述】:
目前我正在从事一个商店管理项目。该项目正在使用 django、django-rest-framework 和 react 构建。我想过滤模型数据。所以我安装了 django-filter。我需要定义像“http://localhost:8000/api/search-products/?category=oil&product=mustard oil”这样的网址。我该怎么做?
我的产品搜索视图是-
class SearchProduct(generics.ListAPIView):
serializer_class = ProductSerializer
filter_backends = [filters.SearchFilter]
search_fields = ["category", "name"]
我的网址是-
path("api/search-product/?<str:category>&<str:name>", SearchProduct.as_view())
显示页面未找到错误。
如何定义合适的url路径?
【问题讨论】:
-
您不必在
urls.py中定义查询参数 -
那它是如何工作的呢?
-
查询参数不应该是路径 url 的一部分。不过它们仍然会被覆盖,它将由 django-filter 为您处理
标签: django django-rest-framework django-urls django-filter