Skip to content

Commit 6fb4f6e

Browse files
committed
Fixed #13765 - 'safe' parameter for urlencode filter
Thanks to KyleMac for the suggestion and SmileyChris for the patch git-svn-id: http://code.djangoproject.com/svn/django/trunk@13849 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 76366aa commit 6fb4f6e

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

django/template/defaultfilters.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,20 @@ def upper(value):
291291
upper.is_safe = False
292292
upper = stringfilter(upper)
293293

294-
def urlencode(value):
295-
"""Escapes a value for use in a URL."""
294+
def urlencode(value, safe=None):
295+
"""
296+
Escapes a value for use in a URL.
297+
298+
Takes an optional ``safe`` parameter used to determine the characters which
299+
should not be escaped by Django's ``urlquote`` method. If not provided, the
300+
default safe characters will be used (but an empty string can be provided
301+
when *all* characters should be escaped).
302+
"""
296303
from django.utils.http import urlquote
297-
return urlquote(value)
304+
kwargs = {}
305+
if safe is not None:
306+
kwargs['safe'] = safe
307+
return urlquote(value, **kwargs)
298308
urlencode.is_safe = False
299309
urlencode = stringfilter(urlencode)
300310

django/utils/http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def urlquote(url, safe='/'):
1414
can safely be used as part of an argument to a subsequent iri_to_uri() call
1515
without double-quoting occurring.
1616
"""
17-
return force_unicode(urllib.quote(smart_str(url), safe))
17+
return force_unicode(urllib.quote(smart_str(url), smart_str(safe)))
1818

1919
urlquote = allow_lazy(urlquote, unicode)
2020

@@ -25,7 +25,7 @@ def urlquote_plus(url, safe=''):
2525
returned string can safely be used as part of an argument to a subsequent
2626
iri_to_uri() call without double-quoting occurring.
2727
"""
28-
return force_unicode(urllib.quote_plus(smart_str(url), safe))
28+
return force_unicode(urllib.quote_plus(smart_str(url), smart_str(safe)))
2929
urlquote_plus = allow_lazy(urlquote_plus, unicode)
3030

3131
def urlencode(query, doseq=0):

docs/ref/templates/builtins.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,6 +1967,19 @@ For example::
19671967
If ``value`` is ``"http://www.example.org/foo?a=b&c=d"``, the output will be
19681968
``"http%3A//www.example.org/foo%3Fa%3Db%26c%3Dd"``.
19691969

1970+
.. versionadded:: 1.1
1971+
1972+
An optional argument containing the characters which should not be escaped can
1973+
be provided.
1974+
1975+
If not provided, the '/' character is assumed safe. An empty string can be
1976+
provided when *all* characters should be escaped. For example::
1977+
1978+
{{ value|urlencode:"" }}
1979+
1980+
If ``value`` is ``"http://www.example.org/"``, the output will be
1981+
``"https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.example.org%2F"``.
1982+
19701983
.. templatefilter:: urlize
19711984

19721985
urlize

tests/regressiontests/templates/filters.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ def get_filter_tests():
265265
'filter-iriencode03': ('{{ url|iriencode }}', {'url': mark_safe('?test=1&me=2')}, '?test=1&me=2'),
266266
'filter-iriencode04': ('{% autoescape off %}{{ url|iriencode }}{% endautoescape %}', {'url': mark_safe('?test=1&me=2')}, '?test=1&me=2'),
267267

268+
# urlencode
269+
'filter-urlencode01': ('{{ url|urlencode }}', {'url': '/test&"/me?/'}, '/test%26%22/me%3F/'),
270+
'filter-urlencode02': ('/test/{{ urlbit|urlencode:"" }}/', {'urlbit': 'escape/slash'}, '/test/escape%2Fslash/'),
271+
268272
# Chaining a bunch of safeness-preserving filters should not alter
269273
# the safe status either way.
270274
'chaining01': ('{{ a|capfirst|center:"7" }}.{{ b|capfirst|center:"7" }}', {"a": "a < b", "b": mark_safe("a < b")}, " A &lt; b . A < b "),

0 commit comments

Comments
 (0)