Code

Ticket #12945: 12945.diff

File 12945.diff, 4.2 KB (added by coleifer, 4 years ago)
Line 
1diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
2index 0db77f1..17990ab 100644
3--- a/django/template/defaulttags.py
4+++ b/django/template/defaulttags.py
5@@ -1085,7 +1085,11 @@ def url(parser, token):
6     This is a way to define links that aren't tied to a particular URL
7     configuration::
8 
9-        {% url path.to.some_view arg1,arg2,name1=value1 %}
10+        {% url path.to.some_view arg1,arg2 %}
11+       
12+        or
13+       
14+        {% url path.to.some_view name1=value1,name2=value2 %}
15 
16     The first argument is a path to a view. It can be an absolute python path
17     or just ``app_name.view_name`` without the project name if the view is
18@@ -1117,27 +1121,26 @@ def url(parser, token):
19     args = []
20     kwargs = {}
21     asvar = None
22-
23-    if len(bits) > 2:
24-        bits = iter(bits[2:])
25-        for bit in bits:
26-            if bit == 'as':
27-                asvar = bits.next()
28-                break
29+    bits = bits[2:]
30+    if len(bits) >= 2 and bits[-2] == 'as':
31+        asvar = bits[-1]
32+        bits = bits[:-2]
33+   
34+    if len(bits):
35+        url_args = ''.join(bits)
36+        end = 0
37+        for i, match in enumerate(url_arg_re.finditer(url_args)):
38+            if (i == 0 and match.start() != 0) or \
39+                  (i > 0 and (url_args[end:match.start()] != ',')):
40+                raise TemplateSyntaxError("Malformed arguments to url tag")
41+            end = match.end()
42+            name, value = match.group(1), match.group(2)
43+            if name:
44+                kwargs[name] = parser.compile_filter(value)
45             else:
46-                end = 0
47-                for i, match in enumerate(url_arg_re.finditer(bit)):
48-                    if (i == 0 and match.start() != 0) or \
49-                          (i > 0 and (bit[end:match.start()] != ',')):
50-                        raise TemplateSyntaxError("Malformed arguments to url tag")
51-                    end = match.end()
52-                    name, value = match.group(1), match.group(2)
53-                    if name:
54-                        kwargs[name] = parser.compile_filter(value)
55-                    else:
56-                        args.append(parser.compile_filter(value))
57-                if end != len(bit):
58-                    raise TemplateSyntaxError("Malformed arguments to url tag")
59+                args.append(parser.compile_filter(value))
60+        if end != len(url_args):
61+            raise TemplateSyntaxError("Malformed arguments to url tag")
62 
63     return URLNode(viewname, args, kwargs, asvar)
64 url = register.tag(url)
65diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
66index 307fecc..ec400d7 100644
67--- a/tests/regressiontests/templates/tests.py
68+++ b/tests/regressiontests/templates/tests.py
69@@ -1033,7 +1033,10 @@ class Templates(unittest.TestCase):
70             'url10': ('{% url regressiontests.templates.views.client_action id=client.id,action="two words" %}', {'client': {'id': 1}}, '/url_tag/client/1/two%20words/'),
71             'url11': ('{% url regressiontests.templates.views.client_action id=client.id,action="==" %}', {'client': {'id': 1}}, '/url_tag/client/1/==/'),
72             'url12': ('{% url regressiontests.templates.views.client_action id=client.id,action="," %}', {'client': {'id': 1}}, '/url_tag/client/1/,/'),
73-            '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/'),
74+            'url13': ('{% url regressiontests.templates.views.client_action id=client.id,action=arg|join:"-" %}', {'client': {'id': 1}, 'arg':['a','b']}, '/url_tag/client/1/a-b/'),
75+            'url14': ('{% url regressiontests.templates.views.client_action id=client.id, action=arg|join:"-" %}', {'client': {'id': 1}, 'arg':['a','b']}, '/url_tag/client/1/a-b/'),
76+            'url15': ('{% url regressiontests.templates.views.client_action client.id, arg|join:"-" %}', {'client': {'id': 1}, 'arg':['a','b']}, '/url_tag/client/1/a-b/'),
77+            'url16': ('{% url regressiontests.templates.views.client_action 12, "test" %}', {}, '/url_tag/client/12/test/'),
78 
79             # Failures
80             'url-fail01': ('{% url %}', {}, template.TemplateSyntaxError),