Attachment '03_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 # Zacneme definiciou triedy
11 class Clovek(object):
12 def __init__(self,meno,pohlavie='M',je_dieta=False,je_dochodca=False):
13 self.meno=meno
14 self.pohlavie=pohlavie
15 self.je_dieta=je_dieta
16 self.je_dochodca=je_dochodca
17
18
19 # Spravime zoznam clovekov
20 zoz=[Clovek('otecko'),
21 Clovek('mamicka',pohlavie='Z'),
22 Clovek('Janko',je_dieta=True),
23 Clovek('Marienka',je_dieta=True),
24 Clovek('jezibaba',pohlavie='Z',je_dieta=False,je_dochodca=True)]
25 # Template si spravime v html
26 t1=Template("""<html>
27 <body>
28 <table>
29 <tr>
30 <th> Meno </th>
31 <th> Pohlavie </th>
32 <th> Dalsie vlastnosti </th>
33 </tr>
34 {% for clovek in zoznam_osob %}
35 <tr>
36 <td> {{ clovek.meno }} </td>
37 <td>
38 {% ifequal clovek.pohlavie 'M' %}
39 muz
40 {% else %}
41 zena
42 {% endifequal %}
43 </td>
44 <td>
45 {% if clovek.je_dieta %}
46 dieta
47 {% endif %}
48 {% if clovek.je_dochodca %}
49 dochodca
50 {% endif %}
51 </td>
52 </tr>
53 {% endfor %}
54 </table>
55 </body>
56 </html>
57 """)
58 # Vytvorime Context
59 c1=Context({'zoznam_osob' : zoz})
60 # A mozeme dosadit
61 import sys
62 sys.stdout.write(t1.render(c1))
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]]
You are not allowed to attach a file to this page.