Home > OS >  Route Flask : Problem of space and accent in a route with variable
Route Flask : Problem of space and accent in a route with variable

Time:04-29

I created an application with Flask.

Locally, everything works.

But now that I have sent to the server, I have a routing problem.

The url that causes the problem is built like this:

@app.route('/analyze/<string:keyword>')

The problem is that the keyword variable can contain accents (éèàç, etc ...) and spaces (example : sol stratifié)

The problem is that instead of laminate floor, I have sol stratifié which appears on the screen, but also when I call the database.

I tried:

  • encode and decode utf- 8 on the keyword variable : it doesn't work
  • import codecs : it doesn't work
  • test to use the function : Flask URL route encoding problems
  • try to add .encode('utf-8') directly in the url route

would you have a solution ?

CodePudding user response:

sol stratifié is a percent-encoded string (URL quoted or %xx escaped in Python terminology).

The urllib.parse module defines functions that fall into two broad categories: URL parsing and URL quoting.

from urllib.parse import unquote
print( unquote( 'sol stratifié'))
sol stratifié
  • Related