Problem with searchbar in Django and HTML

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%C2%A0%. How can I solve?

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

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

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%C2%A013 + Pro".

Back to Top