Fixed #12072 -- Certain characters no longer break url tag. Thanks, Alexander Dutton.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12503 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2010-02-23 05:17:39 +00:00
parent 6ed7bd5609
commit 349827996b
3 changed files with 31 additions and 7 deletions

View File

@ -142,6 +142,7 @@ answer newbie questions, and generally made Django that much better:
Andrew Durdin <adurdin@gmail.com>
dusk@woofle.net
Andy Dustman <farcepest@gmail.com>
Alexander Dutton <dev@alexdutton.co.uk>
J. Clifford Dyer <jcd@sdf.lonestar.org>
Clint Ecker
Nick Efford <nick@efford.org>

View File

@ -1071,6 +1071,13 @@ def templatetag(parser, token):
return TemplateTagNode(tag)
templatetag = register.tag(templatetag)
# Regex for URL arguments including filters
url_arg_re = re.compile(
r"(?:(%(name)s)=)?(%(value)s(?:\|%(name)s(?::%(value)s)?)*)" % {
'name':'\w+',
'value':'''(?:(?:'[^']*')|(?:"[^"]*")|(?:[\w\.-]+))'''},
re.VERBOSE)
def url(parser, token):
"""
Returns an absolute URL matching given view with its parameters.
@ -1118,13 +1125,20 @@ def url(parser, token):
asvar = bits.next()
break
else:
for arg in bit.split(","):
if '=' in arg:
k, v = arg.split('=', 1)
k = k.strip()
kwargs[k] = parser.compile_filter(v)
elif arg:
args.append(parser.compile_filter(arg))
end = 0
for i, match in enumerate(url_arg_re.finditer(bit)):
if (i == 0 and match.start() != 0) or \
(i > 0 and (bit[end:match.start()] != ',')):
raise TemplateSyntaxError("Malformed arguments to url tag")
end = match.end()
name, value = match.group(1), match.group(2)
if name:
kwargs[name] = parser.compile_filter(value)
else:
args.append(parser.compile_filter(value))
if end != len(bit):
raise TemplateSyntaxError("Malformed arguments to url tag")
return URLNode(viewname, args, kwargs, asvar)
url = register.tag(url)

View File

@ -1031,11 +1031,20 @@ class Templates(unittest.TestCase):
'url08': (u'{% url метка_оператора v %}', {'v': 'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
'url09': (u'{% url метка_оператора_2 tag=v %}', {'v': 'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
'url10': ('{% url regressiontests.templates.views.client_action id=client.id,action="two words" %}', {'client': {'id': 1}}, '/url_tag/client/1/two%20words/'),
'url11': ('{% url regressiontests.templates.views.client_action id=client.id,action="==" %}', {'client': {'id': 1}}, '/url_tag/client/1/==/'),
'url12': ('{% url regressiontests.templates.views.client_action id=client.id,action="," %}', {'client': {'id': 1}}, '/url_tag/client/1/,/'),
'url12': ('{% url regressiontests.templates.views.client_action id=client.id,action=arg|join:"-" %}', {'client': {'id': 1}, 'arg':['a','b']}, '/url_tag/client/1/a-b/'),
# Failures
'url-fail01': ('{% url %}', {}, template.TemplateSyntaxError),
'url-fail02': ('{% url no_such_view %}', {}, urlresolvers.NoReverseMatch),
'url-fail03': ('{% url regressiontests.templates.views.client %}', {}, urlresolvers.NoReverseMatch),
'url-fail04': ('{% url view id, %}', {}, template.TemplateSyntaxError),
'url-fail05': ('{% url view id= %}', {}, template.TemplateSyntaxError),
'url-fail06': ('{% url view a.id=id %}', {}, template.TemplateSyntaxError),
'url-fail07': ('{% url view a.id!id %}', {}, template.TemplateSyntaxError),
'url-fail08': ('{% url view id="unterminatedstring %}', {}, template.TemplateSyntaxError),
'url-fail09': ('{% url view id=", %}', {}, template.TemplateSyntaxError),
# {% url ... as var %}
'url-asvar01': ('{% url regressiontests.templates.views.index as url %}', {}, ''),