Answer the question
In order to leave comments, you need to log in
Why doesn't case-insensitive lookup through Q model work in django?
Everything is simple. In models.py there is a model of products and the search is done only by name:
class Product(models.Model):
category = models.ForeignKey(Category, on_delete=models.PROTECT)
brand = models.ForeignKey(Brand, on_delete=models.SET(1), default=1)
title = models.CharField(max_length=120)
...
from django.db.models import Q
def client_search_view(request):
query = request.GET.get('q')
if query != None:
products = Product.objects.filter(Q(title__icontains=query))
context = {
'title': 'Результаты поиска по запросу: ' + query,
'products': products,
}
else:
context = {
'title': 'Введите строку поиска'
}
return render(request, 'client/client_search.html', context=context)
{% extends 'base.html' %}
{% block content %}
<h3 class="text-center">{{ title }}</h3>
<form action="{% url 'client_search' %}">
<div class="form-group">
<input type="text" class="form-control" placeholder="Вот в этой строке" name="q">
<button type="submit" class="btn btn-info"><i class="glyphicon glyphicon-search"></i> Искать</button>
</div>
</form>
{% if products %}
{% for product in products %}
....
{% endfor %}
{% else %}
<h3 class="text-center">По заданному запросу изделия отсутсвуют</h3>
{% endif %}
{% endblock %}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question