Django Reference Sheet
Django Reference Sheet
Italicized Text – Placeholder names for your object(s) • Courier Text – Commands you type in
Template Filters
add - Adds the arg to the value random - Returns a random item from the list
addslashes - Adds slashes, for passing strings to JavaScript. removetags - Removes a space separated list of [X]HTML
Capfirst - Capitalizes the first character of the value tags from the output
center - Centers the value in a field of a given width rjust - Right-aligns the value in a field of a given width,
cut - Removes all values of arg from the given string Argument: field size
date - Formats a dateto the given format using “now” format slice - Returns a slice of the list.
default - If value is unavailable, use given default slugify - Converts to lowercase, removes non-alpha chars
dictsort - Takes a list of dicts, returns that list sorted by the and converts spaces to hyphens
property given in the argument. stringformat - Formats the variable according to the
dictsortreversed - Takes a list of dicts, returns that list sorted in argument, a string formatting specifier. This specifier uses
reverse order by the property given in the argument. Python string formating syntax, with the exception that the
divisibleby - Returns true if the value is divisible by the argument leading "%" is dropped.
escape - Escapes a string's HTML striptags - Strips all [X]HTML tags
filesizeformat - Format the value like a 'human-readable' file. time - Formats a time according to the given format (same
First - Returns the first item in a list as the now tag).
fix_ampersands - Replaces ampersands with & entities timesince - Formats a date as the time since that date
floatformat - Displays a floating point number as 34.2 (with one (i.e. "4 days, 6 hours")
decimal places) - but only if there is a decimal point. title - Converts a string into titlecase
get_digit - Given a whole number, returns the requested digit of it, truncatewords - Truncates a string after a certain number
where 1 is the right-most digit, 2 is the second-right-most digit, etc. of words. Argument: Number of words to truncate after.
Returns the original value for invalid input, unordered_list - Recursively takes a self-nested list and
join - Joins a list with a string, like Python's str.join(list) returns an HTML unordered list -- WITHOUT opening and
length - Returns the length of the value - useful for lists closing <ul> tags.
length_is - Returns a boolean of whether the value's length is the upper - Converts a string into all uppercase
argument urlencode - Escapes a value for use in a URL
linebreaks - Converts newlines into <p> and <br />s urlize - Converts URLs in plain text into clickable links
linebreaksbr - Converts newlines into <br />s urlizetrunc - Converts URLs into clickable links, truncating
linenumbers - Displays text with line numbers URLs to the given character limit. Argument: Length to
ljust - Left-aligns the value in a field of a given width, Argument: truncate URLs to.
field size wordcount - Returns the number of words
lower - Converts a string into all lowercase wordwrap - Wraps words at specified line length.
make_list - Returns the value turned into a list. Argument: number of words to wrap the text at.
phone2numeric - Takes a phone number and converts it in to its yesno - Given a string mapping values for true, false and
numerical equivalent (optionally) None, returns one of those strings according to
pluralize - Returns 's' if the value is not 1. the value. Sample argument "yeah,no,maybe" returns yeah
pprint - A wrapper around pprint.pprint -- for debugging. for true, no for false, and maybe for none.
Template Tag Reference
block – Defines an area that the content can be inserted into. if – Evaluates for true or false
{% block content %}Default Content{% endblock %} {% if athlete %}{{ athlete }}{% endif %}
comment - {% comment %}{% endcomment %} (if statements can have “not” and “or”, but are not allow ed to have “and”)
cycle – Rotates through items ifchanged – Outputs it's content if different from last loop.
<tr class="{% cycle red,blue as colors %}">..</tr> {% ifchanged %}{{ name }}{% endifchanged %}
<tr class="{% cycle colors %}">...</tr> (code w ould be w ithin for loop)
debug – Outputs a bunch of debug info {% debug %} ifnotequal – Checks for equality
extends – Inherits from another template {% ifnotequal v1 v2 %}hi{% endifnotequal %}
{% extends "base" %} load – loads a custom tag set {% load comments %}
(leave off the .html for the template name) now – Outputs current date {% now "jS F Y H:i" %}
filter – Applies the specified filter(s) to the content in between (formatting matches php's date function for the most part)
{% filter lower %}Lowercase this{% endfilter %} regroup –Regroup a list of like objects by an attribute
firstOf – Outputs the first true variable, or nothing if all are false. {% regroup people by gender as grouped %}
{% firstof var1 var2 var3 %} ssi – Includes a file
for – Loop through list item {% ssi /home/html/side.html parsed %}
{% for athlete in athlete_list %} (if the option passed “parsed it w ill be treated as a template)
{{ athlete.name }} templatetag – Used to escape template tags
{% endfor %} {% templatetag openblock %}
forloop.counter = current loop iteration starting at 1 (arguments open/closeblock and open/closevariable)
forloop.counter0 = current loop iteration starting at 0 widthratio – Calculates the ratio of a given value to a
forloop.first = True if first time through loop maximum value, and then applies that ratio to a constant.
forloop.last = True if this is the list time through the loop {% widthratio this_value max_value 100 %}
forloop.parentloop = Parentloop counter (???)
Italicized Text – Placeholder names for your object(s) • Courier Text – Commands you type in
Sample Template
{% extends "base" %}
{% block title %}{{ title|title }}{% endblock %}
{% block intro %}
Default Intro Copy that can be replaced by templates extending this one
{% endblock %}
{% block content %}
{% for entry in blog_entries %}
{% ifchanged %}<h1>{{ entry.pub_date|date”F, y” }}</h1>{% endifchanged %}
<h2>{{ entry.title }}</h2> <p>
{{ entry.body|escape|urlizetrunc:"40"|linebreaks}}</p>
{% endfor %}
{% endblock %}
Most of the Content in this reference sheet was gathered from the http://www.djangoproject.com/ website.
License:
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
http://creativecommons.org/licenses/by-nc-sa/2.5/
Italicized Text – Placeholder names for your object(s) • Courier Text – Commands you type in