Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
45Activity
0 of .
Results for:
No results containing your search query
P. 1
django templates cheat sheet

django templates cheat sheet

Ratings:

4.2

(10)
|Views: 12,615|Likes:
Published by windoze007
A concise 3 page cheat sheet of django's templates
A concise 3 page cheat sheet of django's templates

More info:

Published by: windoze007 on Aug 22, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

09/06/2012

pdf

text

original

 
django_template_cheat.txtPage 1 of 3 Aug/2008
1######################Rendition of django html templates2
from
django.template
import
Context,Template3 t=Template("My name is {{ my_name }}.") 4 c=Context({"my_name": "adrian"}) 5 t.render(c) # outputs "My name is Adrian" 6 c=Context({"my_name": "dolores"}) 7 t.render(c) # outputs "My name is dolores" 89 t=Template("My name is {{ person.first_name }}.") 10# when render meets a "." it will first try person["first_name"]11 c=Context({"person": {"first_name": "Joe", "last_name": "Johnson"}}) 12# then person.first_name13
class
 PersonClass: 
pass
14 p=PersonClass() 15 p.first_name= "Joe" 16 c=Context({"person":p}) 17# then person.first_name()18
class
 PersonClass2:19
def
 first_name(self): 20
return
 "Joe"21 p=PersonClass2() 22 c=Context({"person":p}) 23# then person[first_name]24 c=Context({"person": ["Samantha", "Ron", "Joe"], 'first_name': 2}) 2526 c=Context({"name": "david"}) 27 c['name'] 28
del
c['name'] 29 c['name']="jack" 3031#often you want modules to automatically add some variables into your context:32
from
django.template
import
RequestContext33
def
 extraProcessor(request): 34
return
 {'misc_extra_dictonary': 'here'} 35 c=RequestContext(request, {'name': 'david',},extraProcessor) 36#c will be populated with extra vars37#it will read the settings.py for TEMPLATE_CONTEXT_PROCESSORS and execute them all38#modules are under django.core.context_processors39#include auth:supporting users, debug, i18n, media, request:include request in context40#any function that takes a HttpR
 
equest and returns a dictionary can be used4142#often you want to load html pages:43
from
django.template.loader
import
get_template,select_template44 t=get_template(template_name) #load file or exception 45 t=select_template(template_name_list) #load first in list that exists 46#it will only load from TEMPLATE_DIRS in settings.py4748#often you want to save time not doing all this context, template stuff:49
from
django.template.loader
import
render_to_string50 rendered=render_to_string('my_template.html', { 'foo': 'bar' }) 5152#often you want to write your own filters and tags53#but right now you don't know what filters and tags are yet....5455######################Sample page base_generic.html56{# this is a comment #}5758{%load custom_module1 custom_module2%} 59{# loads custom python filter and tag modules#}6061<html> 62<head> 63<link rel="stylesheet"href="style.css" /> 64<title>{%block title%}David Website{%endblock%}</title>  65{# defining blocks will allow you to replace them later #}
- 1 -
 
django_template_cheat.txtPage 2 of 3 Aug/2008
66</head> 67<body> 68<div id="sidebar"> 69{%block sidebar%} 70<ul> 71<li><a href="/">Home</a></li> 72<li><a href="/blog/">Blog</a></li> 73</ul> 74{%endblock%} 75</div> 7677<div id="content"> 78{%block content%}{%endblock%} 79</div> 8081<div id="disclaimer"> 82{%block disclaimer%}Copyright David{%endblock david%} 83{%include"legals/extraDisclaimer.html" %}  84{# renders that html into this page, passes current context #}85</div> 8687{% 
if
wantDebug%}88{%debug%}{%enddebug%} 89{%endif%} 90</body> 91</html> 9293######################Sample page extended.html94{%extends"base_generic.html" %} 95{# if you render this page, it will render the base_generic.html #}96{# with each block that is defined here replacing that of the parent #}9798{%load custom_module2%} 99{# note that if your parent loads a module, you don't have it #}100101{%block title%}{{section.title}}{%endblock%} 102{# replace the block called "title" in base_generic #}103{# reads variable called section from the context #}104{# searches section["title"],section.title,section.title(),section[title] #}105{# in that exact order #}106107{# unreplaced blocks from base_generic.html, such as 'sidebar' are kept #}108109{%block content%} 110{%spaceless%} {# removes useless whitespace from the html below #} 111<h1>{{section.title}}</h1>  112{% 
for
story
in
story_list%} 113<h2 color:{%cycle'red' 'blue' %}>  114{# each time the tag is read, it will output the next item #}115{{forloop.counter}}  116{# prints the loop counter #}117{# also: counter0, revcounter/0, first, last, parentloop.counter) #}118<a href="{{ story.get_absolute_url }}">  119{{story.headline|upper|capfirst}}  120{# the filter uppercases then captalises first letter #}121</a> 122</h2> 123<p>{{story.tease|cut:"\n"|truncatewords:"100" }}</p>  124{# the filter removes \n and then gets first 100 words #}125{{story.first_comment}}  126{# by default the variable is escaped #}127{# < > ' " & are converted into html character codes #}128129{{story.comment_list|first}}  130{{story.comment_list|join:", "}}
- 2 -

Activity (45)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Bobby Ye liked this
Hanse69 liked this
Hanse69 liked this
Gregory Nicholas liked this
Gregory Nicholas liked this
Arun Karunagath liked this
noirbizarre liked this
boypyt liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->