{% load static humanize event_custom_tags %}

The Humanize Filters

{{ 1|apnumber }}, {{ 5|apnumber }}, {{ 12|apnumber }}
{{ 4500|intcomma }}; {{ 42123456.78|intcomma }}
{{ 4000000|intword }}; {{ 42000000000|intword }}
{{ today|naturalday }}
{{ today|naturaltime }}; {{ past|naturaltime }}; {{ future|naturaltime }}
{{ 1|ordinal }}, {{ 2|ordinal }}, {{ 3|ordinal }}, {{ 20|ordinal }}%} Testing out the Template System

Templates Demo File

Comment Tag

{% comment "this won't show at all in your browser" %}

Three six nine, the goose drank wine

{% endcomment %}

Cycle Tag

{% for i in '12345' %}

Colored Text

{% endfor %}

Cycle tag used to format a table:

{% for i in '12345' %} {% endfor %}
{{ i }}

Resetcycle tag:

{% for i in '12345' %} {% resetcycle %} {% endfor %}
{{ i }}

Filter Tag

{% filter striptags|upper %}

This text will be converted to uppercase and have the HTML tags removed.

{% endfilter %}

Firstof Tag

{% firstof None False '' empty_list 'This one!' 'Not this one' %}

For Tag

List Colors:

The for loop variables:

{% for item in color_list %} {% endfor %}
Item forloop.counter forloop.counter0 forloop.revcounter forloop.revcounter0 forloop.first forloop.last
{{ item }} {{ forloop.counter }} {{ forloop.counter0 }} {{ forloop.revcounter }} {{ forloop.revcounter0 }} {{ forloop.first }} {{ forloop.last }}

For...empty Tag

This list has items to show:

But this one doesn't:

If Tag

{% if empty_list %} This line won't show as list is empty. {% elif color_list %} color_list is not empty, so this line shows. {% else %} Optional else clause. This won't show in this example. {% endif %}
{% if somevar > 5 %} somevar is greater than 5 {% else %} somevar is less than 5 {% endif %}
{% if empty_list and color_list %} Both lists have content {% else%} One list is empty {% endif %}
{% if empty_list or color_list %} At least one list has content {% endif %}
{% if not empty_list %} Negated an empty list, so this will print. {% endif %}
{% if anothervar is True %} This prints if and only if anothervar is True. {% endif %}
{% if novar is None %} This appears if novar is None, or if novar is not found in the context. {% endif %}
{% if anothervar is True %} This prints if and only if anothervar is True. {% endif %}
{% if novar is None %} This appears if novar is None, or if novar is not found in the context. {% endif %}
{% if "dog" in "catdog" %} The best half! {% endif %}
{% if "orange" in color_list %} Orange is in the list! {% else %} Orange is not in the list! {% endif %}
{% if color_list|length < 10 %} List has less than 10 elements {% endif %}
{% if somevar >= 5 and anothervar != 30 %} Both comparisons are true, so this will print. {% endif %}

Lorem Tag

This one will output a number of words:

{% lorem 5 w %}

This one outputs a number of HTML paragraphs:

{% lorem 2 p %}

This one outputs plain text paragraphs:

{% lorem 1 b %}

And finally, we can output a number of random words:

{% lorem 10 w random %}

or random paragraphs:

{% lorem 3 p random %}

Now Tag

{% now "m-d-Y H:i" %}

Regroup Tag

{% regroup best_bands by country as band_list %}

Best Bands:

Url Tag

{% url 'tdemo' %}

Widthratio Tag

With Tag

{% with total_items=color_list|length %} The list has {{ total_items }} item{{ total_items|pluralize }} {% endwith %}

Template Filters

Add Filter

{{ somevar|add:'12' }}
{{ somevar|add:anothervar }}

Addslashes Filter

{{ "I'm not. She didn't."|addslashes }}
{{ 'He said "NO!"'|addslashes }}

Sentence Casing Filters

{{ "the 'capfirst' filter will capitalize the first word"|capfirst }}
{{ "The 'lower' Filter will Convert the Sentence to LOWERCASE"|lower }}
{{ "the 'upper' filter will convert the sentence to uppercase"|upper }}
{{ "and the 'title' filter will convert the sentence to title case"|title }}

Field Alignment Filters

|{{ "Left Justified"|ljust:"30" }}|

|{{ "Centered"|center:"30" }}|

|{{ "Right Justified"|rjust:"30" }}|

Cut Filter

{{ "I really don't like the letter 'e' for some reason"|cut:"e" }}

Formatting Dates and Times

{{ today|date:"l jS E Y" }}
{{ today|time:"H:i" }}

Showing Default Values

{{ empty_list|default:"Empty list" }}
{{ empty_list|default_if_none:"This won't show as Empty != None" }}
{{ None|default_if_none:"Obvious, but you get the idea" }}

Sorting Dictionaries

{{ best_bands|dictsort:"name"}} {{ best_bands|dictsortreversed:"name"}}

Divisibleby Filter

{{ anothervar|divisibleby:"4" }}
{{ anothervar|divisibleby:"3" }}

Filesizeformat Filter

{{ 44040192|filesizeformat }}

Retrieving the First and Last Items from a List

{{ color_list|first }} {{ color_list|last }}

Format Floating Point Numbers

{{ 3.14159265|floatformat }} {{ 3.14159265|floatformat:4 }}

get_digit Filter

{{ anothervar|get_digit:1 }}
{{ anothervar|get_digit:0 }}
{{ "Hello"|get_digit:2 }}

Join Filter

{{ color_list|join:" and " }}

Output JSON

JSON can be output using the json_script filter:

(You will need to look in the page source to see this one)

{{ best_bands|json_script:"best-bands" }} {{ venues|json_script:"all-venues" }}

Length_is Filter

{{ color_list|length_is:"4" }}
{{ empty_list|length_is:"2" }}
{{ "Hello"|length_is:"5" }}

Make_list Filter

{{ "Hello"|make_list }}
{{ "12345"|make_list }}
{{ anothervar|make_list }}
{{ 3.14159265|make_list }}

Phone2numeric Filter

{{ "1800 BITE ME"|phone2numeric }}

Random Filter

(This list will change every time you refresh the browser)

{{ color_list|random }}
{{ color_list|random }}
{{ color_list|random }}

Slice Filter

{{ color_list|slice:":3" }}
{{ color_list|slice:"2:" }}
{{ color_list|slice:":-1" }}
{{ color_list|slice:"1:2" }}

Slugify Filter

{{ "Number of the Beast"|slugify }} {{ "slugify won't use these: /\$#^&"|slugify }}

Stringformat Filter

{{ 314159265|stringformat:"E" }}
{{ 3.14159265|stringformat:".2f" }}

Striptags Filter

{{ "

The Title

"|striptags }}

Calculating the Difference Between Dates

{{ today|timesince:future }} {{ today|timeuntil:past }}

Truncating Strings

{{ "The Title"|truncatechars:7 }}
{{ "The Title"|truncatechars_html:7 }}
{{ "The Title"|truncatewords:1 }}

HTML aware version:

{{ "The Title"|truncatewords_html:1 }}

Unordered Lists

Encoding URLs

{{ "https://bestbands.example.com/bands?album=Master of Puppets&rating=awesome"|urlencode }}

Creating Hyperlinks

{{ "The djangobook.com website is awesome!"|urlize }}
{{ "The djangobook.com website is awesome!"|urlizetrunc:11 }}

Wordwrap Filter

{{ "Now I lay me down to sleep"|wordwrap:4 }}

Yesno Filter

{{ True|yesno }}
{{ False|yesno }}
{{ None|yesno }}
{{ True|yesno:"yeah baby,heck no,meh" }}
{{ False|yesno:"yeah baby,heck no,meh" }}
{{ None|yesno:"yeah baby,heck no,meh" }}

The Humanize Filters

{{ 1|apnumber }}, {{ 5|apnumber }}, {{ 12|apnumber }}
{{ 4500|intcomma }}; {{ 42123456.78|intcomma }}
{{ 4000000|intword }}; {{ 42000000000|intword }}
{{ today|naturalday }}
{{ today|naturaltime }}; {{ past|naturaltime }}; {{ future|naturaltime }}
{{ 1|ordinal }}, {{ 2|ordinal }}, {{ 3|ordinal }}, {{ 20|ordinal }}

Custom Tags and Filters

{{ "James is in rehab again "|reverse }}
{% create_date today %} {% announcements %}