Hello. Site search works only case sensitive. How can I fix it? Search code:

class SearchResultsView(ArticlesView): def get_queryset(self, request): if request.POST.has_key('search'): return self.model.objects.filter( Q(title__icontains=request.POST['search']) | Q(content__icontains=request.POST['search']) ).distinct() return [] def post(self, request): self.object_list = self.get_queryset(request) context = self.get_context_data(object_list=self.object_list) return self.render_to_response(context) 

Help me please)

  • Docks have this way Entry.objects.get (headline__icontains = 'Lennon') - Coveraver
  • it’s strange why icontains doesn’t work as expected, but I’ll say that you don’t need to use a POST request in the search, use GET , the first one is needed only when you want to change the data in the database. - Ivan Semochkin
  • one
    What is your base? In general, your code should work. - FeroxTL

1 answer 1

строка.lower() - removes the register строка.upper() makes uppercase letters. This can be applied in the filter when defining (names for example), and then comparing without a register.

  • But doesn’t icontains, in contrast to contains, have to search for case insensitive? - Coveraver
  • @Coveraver, I wrote a simple and universal way, it works without django, for example, when searching for files without registering - Mihail Ris
  • icontains just do the usual LIKE, which also does not depend on django. It depends only on the support of the database, so LIKE is different for each database there. Your decision is a crutch - FeroxTL