Attachment '02_templates.py'
Download 1 # Druhe interaktivne sedenie: Django Templates
2 #
3 # Pouzitie: V existujucom django projekte
4 # treba spustit "manage.py shell".
5 # To zabezpeci natiahnutie patricnych modulov.
6 #
7 # =====================================================
8 # Najprv import:
9 from django.template import Template,Context
10 # Vytvorime Template,teraz trochu zlozitejsiu
11 t1=Template("""Zoznam zakaznikov firmy {{ nazov_firmy }}
12 {% for zakaznik in zoz_zakaznikov %}
13 * {{ zakaznik }}
14 {% endfor %}
15 """)
16 # Vytvorime Context
17 c1=Context({'nazov_firmy' : 'Zebra s.r.o.', \
'zoz_zakaznikov': ['Jan Prvy', 'Jozef Druhy']}\
)
18 print c1
19 # A mozeme dosadit
20 print t1.render(c1)
21 # Spravme teraz priklad so zoznamom objektov
22 # Zadefinujeme triedu zakaznik
23 class Zakaznik(object):
24 def __init__(self,meno,priezvisko):
25 self.meno=meno
26 self.priezvisko=priezvisko
27
28 c2=Context({'nazov_firmy' : 'Koala a.s.',\
'zoz_zakaznikov' : [Zakaznik('Peter','Prvy'), \
Zakaznik('Ludovit','Sestnasty')]} \
)
29 t2=Template("""Zoznam zakaznikov firmy {{ nazov_firmy }}:
30 {% for zakaznik in zoz_zakaznikov %}
31 * Meno:{{ zakaznik.meno }} Priezvisko:{{ zakaznik.priezvisko }}
32 {% endfor %}
33 """)
34 print t2.render(c2)
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.- [get | view] (2009-02-23 21:48:34, 1.2 KB) [[attachment:02_templates.py]]
- [get | view] (2009-02-23 21:50:15, 1.7 KB) [[attachment:02_templates_out.py]]
You are not allowed to attach a file to this page.