Ticket #12945: 12945.diff

File 12945.diff, 4.2 KB (added by coleifer, 15 years ago)
  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index 0db77f1..17990ab 100644
    a b def url(parser, token):  
    10851085    This is a way to define links that aren't tied to a particular URL
    10861086    configuration::
    10871087
    1088         {% url path.to.some_view arg1,arg2,name1=value1 %}
     1088        {% url path.to.some_view arg1,arg2 %}
     1089       
     1090        or
     1091       
     1092        {% url path.to.some_view name1=value1,name2=value2 %}
    10891093
    10901094    The first argument is a path to a view. It can be an absolute python path
    10911095    or just ``app_name.view_name`` without the project name if the view is
    def url(parser, token):  
    11171121    args = []
    11181122    kwargs = {}
    11191123    asvar = None
    1120 
    1121     if len(bits) > 2:
    1122         bits = iter(bits[2:])
    1123         for bit in bits:
    1124             if bit == 'as':
    1125                 asvar = bits.next()
    1126                 break
     1124    bits = bits[2:]
     1125    if len(bits) >= 2 and bits[-2] == 'as':
     1126        asvar = bits[-1]
     1127        bits = bits[:-2]
     1128   
     1129    if len(bits):
     1130        url_args = ''.join(bits)
     1131        end = 0
     1132        for i, match in enumerate(url_arg_re.finditer(url_args)):
     1133            if (i == 0 and match.start() != 0) or \
     1134                  (i > 0 and (url_args[end:match.start()] != ',')):
     1135                raise TemplateSyntaxError("Malformed arguments to url tag")
     1136            end = match.end()
     1137            name, value = match.group(1), match.group(2)
     1138            if name:
     1139                kwargs[name] = parser.compile_filter(value)
    11271140            else:
    1128                 end = 0
    1129                 for i, match in enumerate(url_arg_re.finditer(bit)):
    1130                     if (i == 0 and match.start() != 0) or \
    1131                           (i > 0 and (bit[end:match.start()] != ',')):
    1132                         raise TemplateSyntaxError("Malformed arguments to url tag")
    1133                     end = match.end()
    1134                     name, value = match.group(1), match.group(2)
    1135                     if name:
    1136                         kwargs[name] = parser.compile_filter(value)
    1137                     else:
    1138                         args.append(parser.compile_filter(value))
    1139                 if end != len(bit):
    1140                     raise TemplateSyntaxError("Malformed arguments to url tag")
     1141                args.append(parser.compile_filter(value))
     1142        if end != len(url_args):
     1143            raise TemplateSyntaxError("Malformed arguments to url tag")
    11411144
    11421145    return URLNode(viewname, args, kwargs, asvar)
    11431146url = register.tag(url)
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index 307fecc..ec400d7 100644
    a b class Templates(unittest.TestCase):  
    10331033            'url10': ('{% url regressiontests.templates.views.client_action id=client.id,action="two words" %}', {'client': {'id': 1}}, '/url_tag/client/1/two%20words/'),
    10341034            'url11': ('{% url regressiontests.templates.views.client_action id=client.id,action="==" %}', {'client': {'id': 1}}, '/url_tag/client/1/==/'),
    10351035            'url12': ('{% url regressiontests.templates.views.client_action id=client.id,action="," %}', {'client': {'id': 1}}, '/url_tag/client/1/,/'),
    1036             '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/'),
     1036            '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/'),
     1037            '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/'),
     1038            'url15': ('{% url regressiontests.templates.views.client_action client.id, arg|join:"-" %}', {'client': {'id': 1}, 'arg':['a','b']}, '/url_tag/client/1/a-b/'),
     1039            'url16': ('{% url regressiontests.templates.views.client_action 12, "test" %}', {}, '/url_tag/client/12/test/'),
    10371040
    10381041            # Failures
    10391042            'url-fail01': ('{% url %}', {}, template.TemplateSyntaxError),
Back to Top