Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#24586 closed Uncategorized (invalid)

@register.inclusion_tag fails to work on decorated function

Reported by: Chris Jerdonek Owned by: nobody
Component: Template system Version: 1.7
Severity: Normal Keywords: template, inclusion_tag, custom tag, decorator
Cc: chris.jerdonek@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When defining custom tags, the @register.inclusion_tag does not seem to work when applied to a decorated function. For example--

@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.

Change History (4)

comment:1 by Chris Jerdonek, 10 years ago

Cc: chris.jerdonek@… added

comment:2 by Preston Timmons, 10 years ago

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.

comment:3 by Claude Paroz, 10 years ago

Resolution: invalid
Status: newclosed

comment:4 by Chris Jerdonek, 10 years ago

Thanks, @prestontimmons. That makes sense.

Note: See TracTickets for help on using tickets.
Back to Top