Fixed #33036 -- Made simple_tag()/inclusion_tag() with takes_context raise TemplateSyntaxError when function has no parameters.

This commit is contained in:
Matt Westcott 2021-08-18 22:11:42 +01:00 committed by Mariusz Felisiak
parent 231de683d8
commit 5092f7247d
4 changed files with 43 additions and 1 deletions

View File

@ -243,7 +243,7 @@ def parse_bits(parser, bits, params, varargs, varkw, defaults,
keyword arguments.
"""
if takes_context:
if params[0] == 'context':
if params and params[0] == 'context':
params = params[1:]
else:
raise TemplateSyntaxError(

View File

@ -150,6 +150,17 @@ def simple_tag_without_context_parameter(arg):
simple_tag_without_context_parameter.anything = "Expected simple_tag_without_context_parameter __dict__"
@register.simple_tag(takes_context=True)
def simple_tag_takes_context_without_params():
"""Expected simple_tag_takes_context_without_params __doc__"""
return 'Expected result'
simple_tag_takes_context_without_params.anything = (
'Expected simple_tag_takes_context_without_params __dict__'
)
@register.simple_tag(takes_context=True)
def escape_naive(context):
"""A tag that doesn't even think about escaping issues"""

View File

@ -242,6 +242,17 @@ def inclusion_tag_without_context_parameter(arg):
inclusion_tag_without_context_parameter.anything = "Expected inclusion_tag_without_context_parameter __dict__"
@register.inclusion_tag('inclusion.html', takes_context=True)
def inclusion_tag_takes_context_without_params():
"""Expected inclusion_tag_takes_context_without_params __doc__"""
return {}
inclusion_tag_takes_context_without_params.anything = (
'Expected inclusion_tag_takes_context_without_params __dict__'
)
@register.inclusion_tag('inclusion_extends1.html')
def inclusion_extends1():
return {}

View File

@ -169,6 +169,16 @@ class SimpleTagTests(TagTestCase):
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.from_string('{% load custom %}{% simple_tag_without_context_parameter 123 %}')
def test_simple_tag_missing_context_no_params(self):
msg = (
"'simple_tag_takes_context_without_params' is decorated with "
"takes_context=True so it must have a first argument of 'context'"
)
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.from_string(
'{% load custom %}{% simple_tag_takes_context_without_params %}'
)
class InclusionTagTests(TagTestCase):
@ -256,6 +266,16 @@ class InclusionTagTests(TagTestCase):
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.from_string('{% load inclusion %}{% inclusion_tag_without_context_parameter 123 %}')
def test_include_tag_missing_context_no_params(self):
msg = (
"'inclusion_tag_takes_context_without_params' is decorated with "
"takes_context=True so it must have a first argument of 'context'"
)
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.from_string(
'{% load inclusion %}{% inclusion_tag_takes_context_without_params %}'
)
def test_inclusion_tags_from_template(self):
c = Context({'value': 42})