diff --git a/AUTHORS b/AUTHORS index 92b1559042..93cf2bdeb0 100644 --- a/AUTHORS +++ b/AUTHORS @@ -142,6 +142,7 @@ answer newbie questions, and generally made Django that much better: Andrew Durdin dusk@woofle.net Andy Dustman + Alexander Dutton J. Clifford Dyer Clint Ecker Nick Efford diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 124841c614..0db77f17af 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -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) diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 22e4e45d98..307feccac0 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -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 %}', {}, ''),