You are on page 1of 2

Template filters

Iss
ue
4
DJANGO CHEAT SHEET default value escape
force_escape

GENERAL
default_if_none value
Version 1.0

ESCAPING
www.mercurytide.co.uk/careers yesno "yes,no,none" safe don’t escape
stringformat "s" escapejs \x20 escapes
python “%” formatting
iriencode IRI to URI
Template tags ... - end tag required
urlencode %20 escapes
first
{# one line comment #} ifequal… var1 var2 last add 5
autoescape… on/off ifnotequal… var1 var2 random divisibleby 3
block… name include "template" length

NUMBERS
floatformat decimal_places
comment… load tag_library length_is number filesizeformat
cycle "one" "two" "three" now "date format" join ", " get_digit n

LISTS
debug regroup list_of_dicts by key as var make_list nth-rightmost digit from integer
extends "template" spaceless… makes list of digits/characters pluralize "y,ies"
filter... filter1|filter2 templatetag openblock slice "1:5"
open or close block, brace, variable, comment date "date_format"
firstof var1 var2 "default" dictsort "key"
url view arg,kwarg=value time "date_format"

& TIMES
dictsortreversed "key"

DATES
for... item in a_list
widthratio abc timesince datetime
if…else…endif boolean expression a÷b×c
unordered_list
adds <li> tags timeuntil datetime
ifchanged… var with… var1.attr as var2

lower
Template date formats upper
h 01 to 12 d 01 to 31 F January T EST, MDT title

TIME
DAY

g 1 to 12 j 1 to 31 M Jan O +0200 capfirst


HOUR

H 00 to 23 S suffix: st, nd, rd or th b jan slugify

MONTH
Z -43200 to 43200 (seconds)
G 0 to 23 Friday N Jan., Feb., March, May ljust width
f 1, 1:30

FORMATS
DAY OF
WEEK

Fri m 01 to 12 rjust width


i 00 to 59 P 1 a.m., 1:30 p.m., noon
& SEC
MIN

0 (Sun) to 6 (Sat) n 1 to 12 center width


s 00 to 59 r Thu, 21 Dec 2000 16:01:07 +0200
wordwrap width
a a.m. or p.m. z day of year: 0 to 365 y 99 L is leap year: True or False

TEXT FORMATTING
YEAR
wordcount
MISC
& PM

MISC
AM

A AM or PM W week number: 1 to 53 Y 1999 t length of month: 28 to 31


striptags
removetags "a img"
ModelAdmin options InlineModelAdmin options truncatewords number
model truncatewords_html number
date_hierarchy ="date_field" list_select_related =False =Book
adds closing tags
form =FormClass inlines =list_of_InlineClasses fk_name ="book"
linebreaks
fieldsets =[("Details", {"fields": ("name",)})] ordering =list_of_field_names formset =BaseInlineFormSet
linebreaksbr
fields =list_of_field_names prepopulated_fields ={"slug": ("name",)} extra =3
urlize
filter_horizontal =False radio_fields ={"agree": admin.VERTICAL} max_num =0
urlizetrunc max_length
filter_vertical =False raw_id_fields =list_of_fk_fields template ="template"
cut "x"
list_display =list_of_field_names save_as =False verbose_name ="Book" strips "x" from string

list_display_links =list_of_field_names save_on_top =False verbose_name_plural ="Books" linenumbers


list_filter =list_of_field_names search_fields =list_of_field_names phone2numeric
list_per_page =100 pprint
Model fields Relational model fields

Iss
ue
4
DJANGO CHEAT SHEET BooleanField DateTimeField ForeignKey(model)
auto_now =False related_name ="model_set"
Version 1.0 NullBooleanField
CharField auto_now_add =False limit_choices_to =query_kwargs
max_length to_field ="key_field"
TimeField
auto_now =False
Model fields common options TextField
auto_now_add =False
ManyToManyField(model)
SlugField related_name ="model_set"
null =False max_length =50 EmailField limit_choices_to =query_kwargs
blank =False max_length =75 through ="IntermediateModel"
FilePathField
choices =list_of_tuples symmetrical =True
path ="/home/images" IPAddressField
db_column ="column_name" match =r"\.jpg$" URLField OneToOneField(model)
db_index =False recursive =True verify_exists =True parent_link ="field"
db_tablespace ="tablespace_name" max_length =100 max_length =200
GenericForeignKey("content_type_field",
default =value_or_func IntegerField FileField "object_id_field")
editable =True PositiveIntegerField upload_to ="uploads/"
help_text ="text" AutoField max_length =100
primary_key =False DecimalField storage =FileSystemStorage
unique =False max_digits =10 Form error_messages keys
ImageField
unique_for_date ="date_field" decimal_places =2 required max_decimal_places
upload_to ="uploads/"
unique_for_month ="date_field" FloatField max_length =100 max_length max_whole_digits
unique_for_year ="date_field" storage =FileSystemStorage min_length missing
SmallIntegerField
height_field ="field_name" invalid empty
PositiveSmallIntegerField invalid_choice invalid_image
width_field ="field_name"
Meta class options CommaSeparatedIntegerField max_value invalid_list
max_length =50 XMLField min_value invalid_link
abstract =False schema_path =path_to_RelaxNG_schema max_digits
DateField
db_table ="table_name" auto_now =False
db_tablespace ="tablespace_name" auto_now_add =False
get_latest_by ="field_name"
order_with_respect_to ="fk_field_name" Form fields
ordering =list_of_columns
BooleanField FloatField ModelChoiceField IPAddressField
permissions =list_of_tuples max_value queryset
NullBooleanField FileField
unique_together =list_of_tuples min_value empty_label ="----"
CharField ImageField
verbose_name ="Model" max_length cache_choices =False
DateField FilePathField
verbose_name_plural ="Models" min_length input_formats =list_of_formats ModelMultipleChoiceField path ="/home/images"
queryset match =r"\.jpg$"
IntegerField DateTimeField
Form fields common options max_value input_formats =list_of_formats
cache_choices =False recursive =False
min_value URLField RegexField
required =True TimeField max_length
input_formats =list_of_formats regex
label ="Field name" DecimalField min_length
max_value max_length
initial ={} ChoiceField verify_exists =False min_length
min_value choices =list_of_tuples validator_user_agent
widget =Widget
max_digits
help_text ="text" decimal_places MultipleChoiceField EmailField Copyright 2008 Mercurytide Ltd.
error_messages ={} choices =list_of_tuples max_length Released under the Creative Commons
min_length Attribution-Share Alike 2.5 UK: Scotland Licence.

You might also like