This decorator can be used to turn simple functions into well-behaved decorators, so long as the decorators are fairly simple. If a decorator expects a function and returns a function (no descriptors), and if it doesn’t modify function attributes or docstring, then it is eligible to use this. Simply apply @simple_decorator to your decorator and it will automatically preserve the docstring and function attributes of functions to which it is applied.
Augments a class’s method with new decorators or documentation.
This is useful when a class needs to add new decorators or new documentation to a parent class’s method, without changing the behavior or burying the existing decorators.
The methods using this decorator can provide code to run at the end of the parent function. Usually, though, it will just have an empty body of
Create a basic template tag with context.
This is similar to Django’s
@register.simple_tagthat optionally takes a context parameter. This condenses many tag implementations down to a few lines of code.
@register.tag @basictag(takes_context=True) def printuser(context): return context['user']
Creates a block template tag with beginning/end tags.
This does all the hard work of creating a template tag that can parse the arguments passed in and then parse all nodes between a beginning and end tag (such as myblock/endmyblock).
By default, the end tag is prefixed with “end”, but that can be changed by passing
end_prefix="end_"or similar to @blocktag.
blocktag will call the wrapped function with context and nodelist parameters, as well as any parameters passed to the tag. It will also ensure that a proper error is raised if too many or too few parameters are passed.
@register.tag @blocktag def divify(context, nodelist, div_id=None): s = "<div" if div_id: s += " id='%s'" % div_id return s + ">" + nodelist.render(context) + "</div>"
Decorator for creating a read-only property that caches a value.
This is a drop-in replacement for Django’s
cached_propertythat retains the docstring and attributes of the original method.
While Django 1.8+ does retain the docstring, it does not retain the attributes.
Decorates a function that returns a URL to add the SITE_ROOT.