Attachment '03_templates_out.py'

Download

   1 $ ./manage.py shell
   2 Python 2.5.2 (r252:60911, Jan  4 2009, 17:40:26)
   3 [GCC 4.3.2] on linux2
   4 Type "help", "copyright", "credits" or "license" for more information.
   5 (InteractiveConsole)
   6 >>> # Druhe interaktivne sedenie: Django Templates
   7 >>> #
   8 >>> # Pouzitie: V existujucom django projekte
   9 >>> # treba spustit "manage.py shell".
  10 >>> # To zabezpeci natiahnutie patricnych modulov.
  11 >>> #
  12 >>> # =====================================================
  13 >>> # Najprv import:
  14 >>> from django.template import Template,Context
  15 >>> # Zacneme definiciou triedy
  16 >>> class Clovek(object):
  17 ...     def __init__(self,meno,pohlavie='M',je_dieta=False,je_dochodca=False):
  18 ...         self.meno=meno
  19 ...         self.pohlavie=pohlavie
  20 ...         self.je_dieta=je_dieta
  21 ...         self.je_dochodca=je_dochodca
  22 ...
  23 >>>
  24 >>> # Spravime zoznam clovekov
  25 >>> zoz=[Clovek('otecko'),
  26 ...     Clovek('mamicka',pohlavie='Z'),
  27 ...     Clovek('Janko',je_dieta=True),
  28 ...     Clovek('Marienka',je_dieta=True),
  29 ...     Clovek('jezibaba',pohlavie='Z',je_dieta=False,je_dochodca=True)]
  30 >>> # Template si spravime v html
  31 >>> t1=Template("""<html>
  32 ... <body>
  33 ... <table>
  34 ... <tr>
  35 ... <th> Meno </th>
  36 ... <th> Pohlavie </th>
  37 ... <th> Dalsie vlastnosti </th>
  38 ... </tr>
  39 ... {% for clovek in zoznam_osob %}
  40 ... <tr>
  41 ... <td> {{ clovek.meno }} </td>
  42 ... <td>
  43 ...     {% ifequal clovek.pohlavie 'M' %}
  44 ...     muz
  45 ...     {% else %}
  46 ...     zena
  47 ...     {% endifequal %}
  48 ... </td>
  49 ... <td>
  50 ...     {% if clovek.je_dieta %}
  51 ...     dieta
  52 ...     {% endif %}
  53 ...     {% if clovek.je_dochodca %}
  54 ...     dochodca
  55 ...     {% endif %}
  56 ... </td>
  57 ... </tr>
  58 ... {% endfor %}
  59 ... </table>
  60 ... </body>
  61 ... </html>
  62 ... """)
  63 >>> # Vytvorime Context
  64 >>> c1=Context({'zoznam_osob' : zoz})
  65 >>> # A mozeme dosadit
  66 >>> import sys
  67 >>> sys.stdout.write(t1.render(c1))
  68 <html>
  69 <body>
  70 <table>
  71 <tr>
  72 <th> Meno </th>
  73 <th> Pohlavie </th>
  74 <th> Dalsie vlastnosti </th>
  75 </tr>
  76 
  77 <tr>
  78 <td> otecko </td>
  79 <td>
  80 
  81     muz
  82 
  83 </td>
  84 <td>
  85 
  86 
  87 </td>
  88 </tr>
  89 
  90 <tr>
  91 <td> mamicka </td>
  92 <td>
  93 
  94     zena
  95 
  96 </td>
  97 <td>
  98 
  99 
 100 </td>
 101 </tr>
 102 
 103 <tr>
 104 <td> Janko </td>
 105 <td>
 106 
 107     muz
 108 
 109 </td>
 110 <td>
 111 
 112     dieta
 113 
 114 
 115 </td>
 116 </tr>
 117 
 118 <tr>
 119 <td> Marienka </td>
 120 <td>
 121 
 122     muz
 123 
 124 </td>
 125 <td>
 126 
 127     dieta
 128 
 129 
 130 </td>
 131 </tr>
 132 
 133 <tr>
 134 <td> jezibaba </td>
 135 <td>
 136 
 137     zena
 138 
 139 </td>
 140 <td>
 141 
 142 
 143     dochodca
 144 
 145 </td>
 146 </tr>
 147 
 148 </table>
 149 </body>
 150 </html>
 151 >>>

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 22:23:15, 1.3 KB) [[attachment:03_templates.py]]
  • [get | view] (2009-02-23 22:25:10, 0.4 KB) [[attachment:03_templates_out.html]]
  • [get | view] (2009-02-23 22:23:26, 2.2 KB) [[attachment:03_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.