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.', \
  22 ...         'zoz_zakaznikov': ['Jan Prvy', 'Jozef Druhy']}\
  23 ...         )
  24 >>> print c1
  25 [{'zoz_zakaznikov': ['Jan Prvy', 'Jozef Druhy'], 'nazov_firmy': 'Zebra s.r.o.'}]
  26 >>> # A mozeme dosadit
  27 >>> print t1.render(c1)
  28 Zoznam zakaznikov firmy Zebra s.r.o.
  29 
  30  * Jan Prvy
  31 
  32  * Jozef Druhy
  33 
  34 
  35 >>> # Spravme teraz priklad so zoznamom objektov
  36 >>> # Zadefinujeme triedu zakaznik
  37 >>> class Zakaznik(object):
  38 ...     def __init__(self,meno,priezvisko):
  39 ...         self.meno=meno
  40 ...         self.priezvisko=priezvisko
  41 ...
  42 >>> c2=Context({'nazov_firmy' : 'Koala a.s.',\
  43 ...         'zoz_zakaznikov' : [Zakaznik('Peter','Prvy'), \
  44 ...             Zakaznik('Ludovit','Sestnasty')]} \
  45 ...             )
  46 >>> t2=Template("""Zoznam zakaznikov firmy {{ nazov_firmy }}:
  47 ... {% for zakaznik in zoz_zakaznikov %}
  48 ...  * Meno:{{ zakaznik.meno }} Priezvisko:{{ zakaznik.priezvisko }}
  49 ... {% endfor %}
  50 ... """)
  51 >>> print t2.render(c2)
  52 Zoznam zakaznikov firmy Koala a.s.:
  53 
  54  * Meno:Peter Priezvisko:Prvy
  55 
  56  * Meno:Ludovit Priezvisko:Sestnasty
  57 
  58 
  59 >>>

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

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