Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33097 closed Uncategorized (invalid)

Class based simple tag that takes context fails with simple_tag().

Reported by: Michael Owned by: nobody
Component: Template system Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

django.template.exceptions.TemplateSyntaxError: 'Foo' is decorated with takes_context=True so it must have a first argument of 'context'

class TextTag(Tag):
    """Return a template tag function for use as a text type tag.

    e.g. <div class="foo">text text text</div>
    Can override the 'render' method
    """
    def render(self, context, text, attrs):
        ...
        return html

    def as_template_tag(self, context, text, **attrs):
        tag = type(self)(self.tag, self.generic_css_class)
        return tag.render(context, text, attrs)

Used as

heading_section = TextTag('h3', 'HeadingSection').as_template_tag
register.simple_tag(heading_section, name='HeadingSection', takes_context=True)

Raises exception:

django.template.exceptions.TemplateSyntaxError: 'HeadingFieldset' is decorated with takes_context=True so it must have a first argument of 'context'

Should This error not be smart enough to see if its a methods, that once bound then context will be the first argument?

Change History (2)

comment:1 by Mariusz Felisiak, 3 years ago

Resolution: invalid
Status: newclosed

It's documented that simple_tag() accepts functions not methods.

comment:2 by Mariusz Felisiak, 3 years ago

Summary: Class based simple tag that takes context fails "must have a first argument of 'context''"Class based simple tag that takes context fails with simple_tag().
Note: See TracTickets for help on using tickets.
Back to Top