Home > OS >  Problem with searchbar in Django and HTML
Problem with searchbar in Django and HTML

Time:10-03

I have created a searchbar in Django and HTML, however, this gives me problems. If I enter "Apple iPhone 13 Pro" in the searchbar it will not find any results, because the URL is not encode. The URL is: Apple iPhone 13 Pro, but it should be: Apple iPhone %. How can I solve?

CodePudding user response:

urls.py

urlpatterns = [
 path('search', Search.as_view(), name='search_fields')
]

views.py

class Search(ListView):
 template_name = 'blog.html'
 paginate_by = 10
 
 def get_queryset(self):
  query = self.request.GET.get('ricerca')
  object_list = Post.objects.filter(Q(titolo_post=query)).order_by('-data')
  return object_list

views.py

CodePudding user response:

I think the reason you don't get the result is not this. Are you sure that your object filtering is working? And be sure that what you get with GET.get() by printing it out.

class Search(ListView):
 template_name = 'blog.html'
 paginate_by = 10
 
 def get_queryset(self):
  query = self.request.GET.get('ricerca')
  print(query,"*"*20)
  object_list = Post.objects.filter(Q(titolo_post__icontains=query)).order_by('-data')
  return object_list

CodePudding user response:

The result I get it if I enter like: "Samsung S22 Ultra". However, if I put "Apple iPhone 13 Pro" as there is a number, it will not find any results, because the URL should be: "Apple iPhone 13 Pro".

  • Related