[Django] #24586: @register.inclusion_tag fails to work on decorated function

24 views
Skip to first unread message

Django

unread,
Apr 5, 2015, 11:16:38 PM4/5/15
to django-...@googlegroups.com
#24586: @register.inclusion_tag fails to work on decorated function
-------------------------+-------------------------------------------------
Reporter: | Owner: nobody
cjerdonek |
Type: | Status: new
Uncategorized |
Component: | Version: 1.7
Template system | Keywords: template,inclusion_tag,custom
Severity: Normal | tag,decorator
Triage Stage: | Has patch: 0
Unreviewed |
Easy pickings: 0 | UI/UX: 0
-------------------------+-------------------------------------------------
When defining custom tags, the `@register.inclusion_tag` does not seem to
work when applied to a decorated function. For example--

{{{
#!python
@register.inclusion_tag('template.html', takes_context=True)
@my_decorator
def my_tag(context, arg):
# Code
}}}

However, I found that if `name='my_tag'` is added to the `inclusion_tag()`
call, then it worked. This wasn't easy to figure out and doesn't seem to
be documented. It just silently failed with no easy way to troubleshoot.
It seems like the tag should work without needing to add the `name`
argument.

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

Django

unread,
Apr 5, 2015, 11:17:38 PM4/5/15
to django-...@googlegroups.com
#24586: @register.inclusion_tag fails to work on decorated function
-------------------------------------+-------------------------------------
Reporter: cjerdonek | Owner: nobody
Type: Uncategorized | Status: new
Component: Template system | Version: 1.7
Severity: Normal | Resolution:
Keywords: | Triage Stage:
template,inclusion_tag,custom | Unreviewed
tag,decorator |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by cjerdonek):

* cc: chris.jerdonek@… (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


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

Django

unread,
Apr 6, 2015, 4:02:41 PM4/6/15
to django-...@googlegroups.com
#24586: @register.inclusion_tag fails to work on decorated function
-------------------------------------+-------------------------------------
Reporter: cjerdonek | Owner: nobody
Type: Uncategorized | Status: new
Component: Template system | Version: 1.7
Severity: Normal | Resolution:
Keywords: | Triage Stage:
template,inclusion_tag,custom | Unreviewed
tag,decorator |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by prestontimmons):

I can see how this would be confusing, but I'm not sure it's a Django
problem.

The key to fixing your example is making sure your decorator uses
`functools.wraps`. This will carry through the `__name__` attribute from
the original `my_tag` function. Otherwise, that piece of information is
lost and the tag will be registered with whatever the name is of the inner
function in your decorator.

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

Django

unread,
Apr 6, 2015, 4:31:50 PM4/6/15
to django-...@googlegroups.com
#24586: @register.inclusion_tag fails to work on decorated function
-------------------------------------+-------------------------------------
Reporter: cjerdonek | Owner: nobody
Type: Uncategorized | Status: closed

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

Keywords: | Triage Stage:
template,inclusion_tag,custom | Unreviewed
tag,decorator |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by claudep):

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


--
Ticket URL: <https://code.djangoproject.com/ticket/24586#comment:3>

Django

unread,
Apr 6, 2015, 5:06:15 PM4/6/15
to django-...@googlegroups.com
#24586: @register.inclusion_tag fails to work on decorated function
-------------------------------------+-------------------------------------
Reporter: cjerdonek | Owner: nobody
Type: Uncategorized | Status: closed
Component: Template system | Version: 1.7
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
template,inclusion_tag,custom | Unreviewed
tag,decorator |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by cjerdonek):

Thanks, @prestontimmons. That makes sense.

--
Ticket URL: <https://code.djangoproject.com/ticket/24586#comment:4>

Reply all
Reply to author
Forward
0 new messages