0

I have a search form that is not returning any results or is giving 404 as some of the linked queries have '%2520' in the URL such as: http://www.website.com/search/?q=some%2520keywords

I'm trying to remove the '%2520' from the query and update the request.GET value at the same time.

This is how I was trying to go about it:

q = { 'q': request.GET.get('q') }
urllib.urlencode(q)
request.GET = request.GET.copy()
request.GET.update(q)
form = form_class(request.GET, searchqueryset=searchqueryset, load_all=load_all)

So far no luck. What am I missing?

WayBehind
  • 1,607
  • 3
  • 39
  • 58

1 Answers1

0

Assuming your form_class correctly queries for the right results:
You are double encoding the url so the space character shows as %2520 instead of %20 as stated in this answer https://stackoverflow.com/a/16085190/4724196

You only need this line from the code above:

form = form_class(request.GET, searchqueryset=searchqueryset, load_all=load_all)

You need to check how your form_class does the filtering and then work according to that, so for example, a basic search that form view that removes spaces works like this:

  1. split the search query into an array of keywords:

    search = self.request.GET.get('q')
    kw_list = search.split()

  2. loop through the keywords while filtering for the right results, an example would be like this :

    Poll.objects.get(Q(title__icontains=keyword) for keyword in kw_list))

it all depends on how you want to filter for the results, so to remove the white spaces just do .split()

Community
  • 1
  • 1
HassenPy
  • 2,083
  • 1
  • 16
  • 31
  • I understand how that is happening, but that is not the solution to my issue as Google bot is indexing those links incorrectly and is giving me 404 errors. That's the only reason I'm trying to remove the empty spaces from the url query and return a correct search results. – WayBehind Dec 01 '15 at 19:26
  • so when you remove those lines all you get is a %20 which is a space, then you want to remove that if i'm correct – HassenPy Dec 01 '15 at 19:46
  • Correct, I need to give the search form a clean phrase with no `%2520` and no `%20` otherwise the search page will return no results. – WayBehind Dec 01 '15 at 20:35
  • Thanks for the update. While your solution may be functional, I'm afraid I won't be able to use it as my search is powered by Haystack, therefore, all I really need is a clean keyword without any special characters. That was the only reason I was trying to update the url keyword encoding. – WayBehind Dec 01 '15 at 23:16