From 5092f7247de8d3aa2fdb762e50f5ac2aa4e6f6e0 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Wed, 18 Aug 2021 22:11:42 +0100 Subject: [PATCH] Fixed #33036 -- Made simple_tag()/inclusion_tag() with takes_context raise TemplateSyntaxError when function has no parameters. --- django/template/library.py | 2 +- tests/template_tests/templatetags/custom.py | 11 ++++++++++ .../template_tests/templatetags/inclusion.py | 11 ++++++++++ tests/template_tests/test_custom.py | 20 +++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/django/template/library.py b/django/template/library.py index 69fdeafc3b..be45331309 100644 --- a/django/template/library.py +++ b/django/template/library.py @@ -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( diff --git a/tests/template_tests/templatetags/custom.py b/tests/template_tests/templatetags/custom.py index 8efd70147a..0880e6fad4 100644 --- a/tests/template_tests/templatetags/custom.py +++ b/tests/template_tests/templatetags/custom.py @@ -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""" diff --git a/tests/template_tests/templatetags/inclusion.py b/tests/template_tests/templatetags/inclusion.py index 21b4b10a6e..45281b4c56 100644 --- a/tests/template_tests/templatetags/inclusion.py +++ b/tests/template_tests/templatetags/inclusion.py @@ -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 {} diff --git a/tests/template_tests/test_custom.py b/tests/template_tests/test_custom.py index 62db0d44bc..cade3e4610 100644 --- a/tests/template_tests/test_custom.py +++ b/tests/template_tests/test_custom.py @@ -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})