1
Я хочу создать форму поиска на моем сайте django. Так что я просто создать список поиска продукта:Django search html form
class ProductSearchListView(ProductList):
"""
Display a Product List page filtered by the search query
"""
paginate_by = 10
def get_queryset(self):
result = super(ProductSearchListView, self).get_queryset()
query = self.request.GET.get('q')
if query:
query_list = query.split()
result = result.filter(
reduce(
operator.and_,
(Q(nome__icontains=q) for q in query_list)
) |
reduce(
operator.and_,
(Q(categoria__icontains=q) for q in query_list)
)
)
return result
В моем HTML я хочу иметь поле, чтобы сделать простой поиск и, нажав кнопку ENTER, пользователь должен увидеть список всех товаров.
<form id="searchform" action="{% do something %}" method="get" accept-charset="utf-8">
<input .../>
</form>
{% if list_of_products_returned_from_search == null %}
<div ...>
None product found.
</div>
{% elif products exists %}
{% for product in products %}
Name: {{product.name}}
{% endfor %}
{% endif %}
Итак, в чем вопрос? ' karthikr
Вопрос: «Как вызвать метод query_set» в html? – dsbonafe
@ user2732966 зависит от того, как он сохраняется в контексте. Что входит в ProductList? – zanderle