Disabling character escaping in the Flask url_for function

Is there a way for Flask to url_fordisable auto-adaptation? Therefore, if I have an endpoint with a name getUserwith this path:, /user/<userID>I want to call url_for('getUser', userID='%')and return it /user/%. Currently, it will exit% symobl and issue /user/%25. I want to do this because I url_forhave to run it at compile time, but the final URL is created when the javscript script is run. I will use the javascript string substitution method to convert /user/%to /user/abcd, but the script substitution that I use requires the use of a character %as a placeholder.

+4
source share
1 answer

url_fordoes not support your use case, but if you use it inside the Jinja template, you can simply add a call replaceto remove the encoding:

{{ url_for('get_user', user_id='%') | replace('%25', '%') }}

Alternatively, if you pass the URL in plain Python code, you can use urllib.parse.unquote(or urllib.unquoteif you are still in Python 2):

url = url_for('get_user', 'user_id'='%')
url = unquote(url)
+5
source

All Articles