django-utils v0.3.0 documentation

This Page


All templatetags are included in a library named djutils_tags. There are a handful of tags related to creating dynamic formsets, numerous filters for grabbing querysets dynamically, and a couple other miscellaneous filters.

Dynamic FormSets

dynamic_formset(formset, fields)

Wraps up all of the formset filters to generate a complete dynamic FormSet for the given fields.


<script type="text/javascript" src="/path/to/djutils/media/javascript/formset.js"></script>
<script type="text/javascript">
    $(function() {
        FormSet('{{ formset.prefix }}');


{{ formset|dynamic_formset }}
{{ formset|dynamic_formset:"field1,field2,etc" }}


<thead><tr class="header-row form">

<tr class="empty-row form">
  <td><input type="text" name="form-__prefix__-name" id="id_form-__prefix__-name" /></td><td><select name="form-__prefix__-choice" id="id_form-__prefix__-choice">
<option value="1">One</option>
<option value="2">Two</option>
  <td><a href="javascript:void(0)" class="form-delete-row">Remove</a></td>

<tr class="dynamic-form form">
    <td><input type="text" name="form-0-name" id="id_form-0-name" /></td><td><select name="form-0-choice" id="id_form-0-choice">
<option value="1">One</option>
<option value="2">Two</option>

<tr id="form-add-row">
  <td colspan="3"><a href="javascript:void(0)" class="form-add-row">Add row</a></td>

Looking closer, there are 4 “distinct” chunks of markup generated. The dynamic_formset() function wraps up the following filters which are responsible for generating each chunk of markup:

dynamic_formset_header_row(formset, fields)
dynamic_formset_empty_row(formset, fields)
dynamic_formset_forms(formset, fields)
dynamic_formset_add_row(formset, colspan)

Model Filters

A collection of filters that operates on querysets or models.


if the model uses a PublishedManager then these filters will operate on the published() instances as opposed to all()

latest(model_or_qs, date_field='id')

Given a model string or a queryset, return the ‘newest’ instances based on the provided field (default is “id”)


{% for obj in ""|latest:"pub_date"|slice:":5" %}
  ... iterate over the 5 newest photos ...
{% endfor %}
alpha(model, field='title')

Given a model string or a queryset, return the instances ordered alphabetically on the provided field (default is “title”)


{% for obj in "blog.entries"|alpha:"title" %}
  ... iterate over blog entries alphabetically ...
{% endfor %}
call_manager(model_or_obj, method)

Given a model or object, call a manager method


{% for obj in "blog.entries"|call_manager:"published" %}
{% endfor %}
tumble(models_and_dates, limit=5)

Generate a tumble for one or more models. Useful for creating a list comprised of various model classes.

The first argument, models_and_dates, is a comma-separated list of models. Each model can optionally define a field to sort by, separated by a colon, i.e. “app.model_name:field”


{% for activity in "blog.entries:pub_date,github.commit:commit_date"|tumble:5 %}
  {{ show the 5 most recent things I did }}
{% endfor %}

Miscellaneous Filters


Automatically syntax-highlight text between <code> tags.


{{ entry.body|syntax_highlight|linebreaks }}
gravatar(email, size=80)

Return the url for a gravatar given an email address

as_template(obj, template=None)

Render a model instance using the given template, defaulting to includes/*app_label.module_name*.html


{% for obj in object_list %}
  <li>{{ obj|as_template }}</li>
{% endfor %}