[Django] #33097: Class based simple tag that takes context fails "must have a first argument of 'context''"

30 views
Skip to first unread message

Django

unread,
Sep 9, 2021, 8:33:23 AM9/9/21
to django-...@googlegroups.com
#33097: Class based simple tag that takes context fails "must have a first argument
of 'context''"
-------------------------------------------+------------------------
Reporter: Michael | Owner: nobody
Type: Uncategorized | Status: new
Component: Template system | Version: 3.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
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?

--
Ticket URL: <https://code.djangoproject.com/ticket/33097>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 9, 2021, 10:34:25 AM9/9/21
to django-...@googlegroups.com
#33097: Class based simple tag that takes context fails "must have a first argument
of 'context''"
---------------------------------+--------------------------------------
Reporter: Michael | Owner: nobody
Type: Uncategorized | Status: closed

Component: Template system | Version: 3.2
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/33097#comment:1>

Django

unread,
Sep 9, 2021, 10:34:52 AM9/9/21
to django-...@googlegroups.com
#33097: Class based simple tag that takes context fails with simple_tag().
---------------------------------+--------------------------------------
Reporter: Michael | Owner: nobody
Type: Uncategorized | Status: closed

Component: Template system | Version: 3.2
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/33097#comment:2>

Reply all
Reply to author
Forward
0 new messages