Jump to >

This documentation covers Djblets 2.0. You can see the latest Djblets documentation or all other versions.



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']

Deprecated since version 1.0: Use django.template.Library.simple_tag() instead.

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.

  • end_prefix (unicode, optional) – The prefix for the end tag. This defaults to 'end', but template tags using underscores in the name might want to change this to 'end_'.
  • resolve_vars (bool, optional) – Whether to automatically resolve all variables provided to the template tag. By default, variables are resolved. Template tags can turn this off if they want to handle variable parsing manually.

The resulting template tag function.

Return type:



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.
optional_decorator(decorator, predicate)[source]

Optionally apply a decorator given a predicate function.

  • decorator (callable) – The decorator to conditionally apply.
  • predicate (callable) – The predicate used to determine when decorator should be used. The arguments provided to the decorated function will be passed to this function.

A decorator that, when applied to a function, will call the function decorated with decorator when predicate() returns True. Otherwise it will call the original function.

Return type:



from djblets.util.decorators import (optional_decorator,

def predicate(verbose)
    return verbose

def decorator(f):
    def decorated(verbose):
        print('Hello from the decorator')
        return f(verbose)

@optional_decorator(decorator, predicate)
def f(x):
     print('Hello from f()')

# Prints "Hello from the decorator" and "Hello from f()"

# Prints "Hello from f()"