cdonts
cdonts

Reputation: 9599

GET search in multilanguage site

I've included a search form in my web2py application, in the following form:

myapp/controller/search?query=myquery

However, for security reasons web2py automatically replaces spaces and non-alphanumeric characters with underscores, which is okay for English-only sites but an impediment for languages that use accent marks. For example, searching for "áéíóú" returns five underscores.

This could be solved by using POST instead of GET for the search form, but then the users wouldn't be able to bookmark the results.

Is there any option to solve this?

Thanks in advance.

Upvotes: 1

Views: 81

Answers (2)

minocha
minocha

Reputation: 1044

This is a general problem people face while handling urls. You can use the quote/quote_plus module in urllib to normalize the strings -

For example, from the strings you suggested -

>>> print  urllib.quote('éíóú')
%C3%A9%C3%AD%C3%B3%C3%BA
>>> print  urllib.unquote('%C3%A9%C3%AD%C3%B3%C3%BA')
éíóú

you will have to perform the unquote when you retrieve it on the backend from the request.

There are also some other posts which might be helpful - urlencode implementation and unicode ready urls

Upvotes: 1

fips
fips

Reputation: 4379

Here's an idea that I've used in the past:

  1. Use post to submit the query
  2. Generate a unique string (e.g. youtube: https://www.youtube.com/watch?v=jX3DuS2Ak3g)
  3. Associate the query to that string and store as key/value pair in session/app state/db (depending on how long you want it to live)
  4. Redirect the user to that

If you don't want to occupy extra memory/space as they tend to grow a lot in some cases, you can substitute steps 2-3 with encrypting the string to something you can decrypt afterwards. You can do this in a middleware class so that it's transparent to your app's logic.

Upvotes: 1

Related Questions