Ticket #12068: 12068.diff

File 12068.diff, 3.6 KB (added by Stefan Moluf, 15 years ago)

Same patch, but with some nitpicky style changes

  • django/template/defaulttags.py

     
    11181118    if len(bits) > 2:
    11191119        bits = iter(bits[2:])
    11201120        for bit in bits:
     1121
    11211122            if bit == 'as':
    1122                 asvar = bits.next()
    1123                 break
    1124             else:
    1125                 for arg in bit.split(","):
    1126                     if '=' in arg:
    1127                         k, v = arg.split('=', 1)
    1128                         k = k.strip()
    1129                         kwargs[k] = parser.compile_filter(v)
    1130                     elif arg:
    1131                         args.append(parser.compile_filter(arg))
     1123                try:
     1124                    asvar = bits.next()
     1125
     1126                    # "as" cannot be the argument of the 'as' keyword, because it is
     1127                    # ambiguous:
     1128                    #
     1129                    #    {% url view as as %}
     1130                    #
     1131                    # Meaning 1. This is an 'as' variable and an incomplete 'as' clause
     1132                    # Meaning 2. There is a complete 'as' clause and no variables
     1133
     1134                    if asvar == 'as':
     1135                        raise TemplateSyntaxError("'as' may not be used as the argument"
     1136                                                  " to an 'as' keyword")
     1137
     1138                    # We break out of this loop once we've processed an 'as' clause
     1139                    break
     1140
     1141                except StopIteration:
     1142                    # An incomplete 'as' clause is an error
     1143                    raise TemplateSyntaxError("'as' must be followed by an argument in"
     1144                                              " the 'url' template tag")
     1145
     1146
     1147            for arg in bit.split(","):
     1148                if '=' in arg:
     1149                    k, v = arg.split('=', 1)
     1150                    k = k.strip()
     1151                    kwargs[k] = parser.compile_filter(v)
     1152                elif arg:
     1153                    args.append(parser.compile_filter(arg))
     1154
    11321155    return URLNode(viewname, args, kwargs, asvar)
    11331156url = register.tag(url)
    11341157
  • tests/regressiontests/templates/tests.py

     
    181181        settings.SETTINGS_MODULE = old_settings_module
    182182        settings.TEMPLATE_DEBUG = old_template_debug
    183183
     184    def test_url_incomplete_as_clause(self):
     185        # Regression test for #12068
     186        from django.template import Template, TemplateSyntaxError
     187
     188        # The 'as' keyword is reserved in the url tag, and must be followed by a name
     189        # (which is not 'as')
     190
     191        self.assertRaises(TemplateSyntaxError, Template, '{% url view as %}')
     192        self.assertRaises(TemplateSyntaxError, Template, '{% url view as as %}')
     193
    184194    def test_templates(self):
    185195        template_tests = self.get_template_tests()
    186196        filter_tests = filters.get_filter_tests()
  • docs/ref/templates/builtins.txt

     
    947947      <a href="{{ the_url }}">Link to optional stuff</a>
    948948    {% endif %}
    949949
     950Note that "as" is a reserved keyword, which can't be used as a variable name or as
     951the value of ``var``, as below::
     952
     953    {% url path.to.view as %}
     954    {% url path.to.view as as %}
     955
     956
    950957.. versionadded:: 1.1
    951958
    952959If you'd like to retrieve a namespaced URL, specify the fully qualified name::
Back to Top