Reputation: 778
I got this code that works to make active search. I have it working but the problem is that it cannot search well. If it's a small letter and you type capital it doesn't recognize. It is case sensitive. How can I make it case insensitive???
@require_http_methods(['POST'])
def search(request):
res_todos = []
search = request.POST['search']
if len(search) == 0:
return render(request, 'todo.html', {'todos':[]})
for i in list(Persons.objects.values()):
if search in i['title']:
res_todos.append(i)
return render(request, 'todo.html', {'todos': res_todos})
Upvotes: 1
Views: 297
Reputation: 477641
You can let the database do the work for you and thus filter with:
@require_http_methods(['POST'])
def search(request):
search = request.POST['search']
qs = Persons.objects.none()
if search:
qs = Persons.objects.filter(title__icontains=search)
return render(request, 'todo.html', {'todos': qs})
Usually for searching with a query, a GET request is used instead of a POST request.
Note: normally a Django model is given a singular name, so
Person
instead of.Persons
Upvotes: 3