Does Flask's url_for
method have an option to disable autoescaping? So if I have an endpoint called getUser
with a route like this: /user/<userID>
, I want to call url_for('getUser', userID='%')
and have it return /user/%
. Currently it will escape the % symobl and give out /user/%25
. I want to do that because url_for
has to run at template compile-time, but the final URL is composed when a javscript script runs. I will be using a javascript string substitution method to convert /user/%
into /user/abcd
, but the substitution script I'm using requires you to use a %
symbol as the placeholder.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
url_for
does not support your use case, but assuming you are using it inside a Jinja template you could just add a call to replace
to remove the encoding:
{{ url_for('get_user', user_id='%') | replace('%25', '%') }}
Alternatively, if you passing the URL around in normal Python code you could use urllib.parse.unquote
(or urllib.unquote
if you are still on Python 2):
url = url_for('get_user', 'user_id'='%')
url = unquote(url)