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.', \
  18         'zoz_zakaznikov': ['Jan Prvy', 'Jozef Druhy']}\
  19         )
  20 print c1
  21 # A mozeme dosadit
  22 print t1.render(c1)
  23 # Spravme teraz priklad so zoznamom objektov
  24 # Zadefinujeme triedu zakaznik
  25 class Zakaznik(object):
  26     def __init__(self,meno,priezvisko):
  27         self.meno=meno
  28         self.priezvisko=priezvisko
  29 
  30 c2=Context({'nazov_firmy' : 'Koala a.s.',\
  31         'zoz_zakaznikov' : [Zakaznik('Peter','Prvy'), \
  32             Zakaznik('Ludovit','Sestnasty')]} \
  33             )
  34 t2=Template("""Zoznam zakaznikov firmy {{ nazov_firmy }}:
  35 {% for zakaznik in zoz_zakaznikov %}
  36  * Meno:{{ zakaznik.meno }} Priezvisko:{{ zakaznik.priezvisko }} 
  37 {% endfor %}
  38 """)
  39 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]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.