Jump to >



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


Create a basic template tag with context.

This is similar to Django’s @register.simple_tag that optionally takes a context parameter. This condenses many tag implementations down to a few lines of code.

For example:

def printuser(context):
    return context['user']
blocktag(*args, **kwargs)[source]

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.

For example:

def divify(context, nodelist, div_id=None):
    s = "<div"

    if div_id:
        s += " id='%s'" % div_id

    return s + ">" + nodelist.render(context) + "</div>"
class cached_property(func)[source]

Bases: django.utils.functional.cached_property

Decorator for creating a read-only property that caches a value.

This is a drop-in replacement for Django’s cached_property that retains the docstring and attributes of the original method.

While Django 1.8+ does retain the docstring, it does not retain the attributes.


Initialize the property.

Parameters:func (callable) – The function that will be called when this property is accessed. The property will have its name, documentation, and other attributes.

Decorates a function that returns a URL to add the SITE_ROOT.