Attachment '02_templates_out.py'

Download

   1 Python 2.5.2 (r252:60911, Jan  4 2009, 17:40:26)
   2 [GCC 4.3.2] on linux2
   3 Type "help", "copyright", "credits" or "license" for more information.
   4 (InteractiveConsole)
   5 >>> # Druhe interaktivne sedenie: Django Templates
   6 >>> #
   7 >>> # Pouzitie: V existujucom django projekte
   8 >>> # treba spustit "manage.py shell".
   9 >>> # To zabezpeci natiahnutie patricnych modulov.
  10 >>> #
  11 >>> # =====================================================
  12 >>> # Najprv import:
  13 >>> from django.template import Template,Context
  14 >>> # Vytvorime Template,teraz trochu zlozitejsiu
  15 >>> t1=Template("""Zoznam zakaznikov firmy {{ nazov_firmy }}
  16 ... {% for zakaznik in zoz_zakaznikov %}
  17 ...  * {{ zakaznik }}
  18 ... {% endfor %}
  19 ... """)
  20 >>> # Vytvorime Context
  21 >>> c1=Context({'nazov_firmy' : 'Zebra s.r.o.', \
...         'zoz_zakaznikov': ['Jan Prvy', 'Jozef Druhy']}\
...         )
  22 >>> print c1
  23 [{'zoz_zakaznikov': ['Jan Prvy', 'Jozef Druhy'], 'nazov_firmy': 'Zebra s.r.o.'}]
  24 >>> # A mozeme dosadit
  25 >>> print t1.render(c1)
  26 Zoznam zakaznikov firmy Zebra s.r.o.
  27 
  28  * Jan Prvy
  29 
  30  * Jozef Druhy
  31 
  32 
  33 >>> # Spravme teraz priklad so zoznamom objektov
  34 >>> # Zadefinujeme triedu zakaznik
  35 >>> class Zakaznik(object):
  36 ...     def __init__(self,meno,priezvisko):
  37 ...         self.meno=meno
  38 ...         self.priezvisko=priezvisko
  39 ...
  40 >>> c2=Context({'nazov_firmy' : 'Koala a.s.',\
...         'zoz_zakaznikov' : [Zakaznik('Peter','Prvy'), \
...             Zakaznik('Ludovit','Sestnasty')]} \
...             )
  41 >>> t2=Template("""Zoznam zakaznikov firmy {{ nazov_firmy }}:
  42 ... {% for zakaznik in zoz_zakaznikov %}
  43 ...  * Meno:{{ zakaznik.meno }} Priezvisko:{{ zakaznik.priezvisko }}
  44 ... {% endfor %}
  45 ... """)
  46 >>> print t2.render(c2)
  47 Zoznam zakaznikov firmy Koala a.s.:
  48 
  49  * Meno:Peter Priezvisko:Prvy
  50 
  51  * Meno:Ludovit Priezvisko:Sestnasty
  52 
  53 
  54 >>>

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.

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