There is a urlencode
filter in Jinja, which can be used with {{ url | urlencode }}
, but I'm looking for a "plus" version that replaces spaces with +
instead of %20
, like urllib.quote_plus()
. Is there anything off the shelf or is it a time for a custom filter?
Asked
Active
Viewed 1.2k times
15

Mark Amery
- 143,130
- 81
- 406
- 459

disruptive
- 5,687
- 15
- 71
- 135
1 Answers
19
No, Jinja2 does not have a built-in method that functions like quote_plus
; you will need to create a custom filter.
Python
from flask import Flask
# for python2 use 'from urllib import quote_plus' instead
from urllib.parse import quote_plus
app = Flask('my_app')
app.jinja_env.filters['quote_plus'] = lambda u: quote_plus(u)
HTML
<html>
{% set url = 'http://stackoverflow.com/questions/33450404/quote-plus-urlencode-filter-in-jinja' %}
{{ url|quote_plus }}
</html>

Mark Amery
- 143,130
- 81
- 406
- 459

Wondercricket
- 7,651
- 2
- 39
- 58
-
I do get an issue with this, and that is the following when applying for example |quote_plus to `XXX%20YYY`. I get an error as follows: /app/.heroku/python/lib/python2.7/urllib.py:1303: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal – disruptive Nov 04 '15 at 14:47
-
@Navonod you are possibly using a mixture of `str` and `unicode`. Take a look at this question for more information. http://stackoverflow.com/questions/18193305/python-unicode-equal-comparison-failed – Wondercricket Nov 04 '15 at 14:53
-
I have converted now using .encode("utf-8"), which means the quote_plus now works, but now I'm getting a nasty b'string' in my results – disruptive Nov 04 '15 at 17:08
-
@disruptive That makes... no sense at all. Your error clearly indicates you're in Python 2, but the `b'...'` representation of byte strings is only used for display in Python 3, to the best of my knowledge; Python 2.7 will understand `b'foo'` as a synonym of `'foo'` but will never choose to print a string with the `b` prefix. Unless you're using some `__future__` imports that change that, perhaps? An MCVE might make for an interesting `python2` question. – Mark Amery Apr 27 '19 at 21:19
-
3Why not use `env.filters['quote_plus'] = quote_plus`? – kevr Jun 17 '21 at 11:22