Python-Ref > Dynamic web pages in Python > Jinja > Template inheritance
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Template inheritance

How to create several templates based on one base template.
Expand/Shrink
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>My fantastic web - {% block title %}{% endblock %}</title>
</head>
<body>
<h1 style="background-color: #beda42; text-align: center;">My fantastic web</h1>
<h2>{% block subtitle %}{% endblock %}</h2>
{% block content %}
{% endblock %}
<pre style="border-top: solid 1px black">Copyright @ 2009 Beda Kosata</pre>
</body>
</html>
{% extends 'page_template2.html' %}

{% block title %}Countries{% endblock %}

{% block subtitle %}All countries{% endblock %}

{% block content %}
<table>
{% for country in countries %}
<tr><td>{{ country['Name'] }}</td><td>{{ country['Population'] }}</td></tr>
{% endfor %}
</table>
{% endblock %}
Zdroj: (jinja3-1.py)
  1   import MySQLdb
  2   from MySQLdb.cursors import DictCursor
  3   import jinja2
  4   env = jinja2.Environment(loader=jinja2.FileSystemLoader(["/home/beda/projects/python-ref/_tmp/infiles"])) 
  5   
  6   def index():
  7     connection = MySQLdb.connect( user="test", passwd="pass123", db="world", host="localhost", cursorclass=DictCursor, use_unicode=True)
  8     cursor1 = connection.cursor()
  9   
 10     cursor1.execute( "SELECT Name,Population FROM Country ORDER BY Name;")
 11     countries = cursor1.fetchall()
 12     cursor1.close()
 13     connection.close()
 14   
 15     template = env.get_template( "page_template2_countries.html")
 16     return template.render( title="Country list", countries=countries)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>My fantastic web - Countries</title>
</head>
<body>
<h1 style="background-color: #beda42; text-align: center;">My fantastic web</h1>
<h2>All countries</h2>

<table>

<tr><td>Afghanistan</td><td>22720000</td></tr>

<tr><td>Albania</td><td>3401200</td></tr>

<tr><td>Algeria</td><td>31471000</td></tr>

<tr><td>American Samoa</td><td>68000</td></tr>

<tr><td>Andorra</td><td>78000</td></tr>

<tr><td>Angola</td><td>12878000</td></tr>

<tr><td>Anguilla</td><td>8000</td></tr>

<tr><td>Antarctica</td><td>0</td></tr>

<tr><td>Antigua and Barbuda</td><td>68000</td></tr>

<tr><td>Argentina</td><td>37032000</td></tr>

<tr><td>Armenia</td><td>3520000</td></tr>

<tr><td>Aruba</td><td>103000</td></tr>

<tr><td>Australia</td><td>18886000</td></tr>

<tr><td>Austria</td><td>8091800</td></tr>

<tr><td>Azerbaijan</td><td>7734000</td></tr>

<tr><td>Bahamas</td><td>307000</td></tr>

<tr><td>Bahrain</td><td>617000</td></tr>

<tr><td>Bangladesh</td><td>129155000</td></tr>

<tr><td>Barbados</td><td>270000</td></tr>

<tr><td>Belarus</td><td>10236000</td></tr>

<tr><td>Belgium</td><td>10239000</td></tr>

<tr><td>Belize</td><td>241000</td></tr>

<tr><td>Benin</td><td>6097000</td></tr>

<tr><td>Bermuda</td><td>65000</td></tr>

<tr><td>Bhutan</td><td>2124000</td></tr>

<tr><td>Bolivia</td><td>8329000</td></tr>

<tr><td>Bosnia and Herzegovina</td><td>3972000</td></tr>

<tr><td>Botswana</td><td>1622000</td></tr>

<tr><td>Bouvet Island</td><td>0</td></tr>

<tr><td>Brazil</td><td>170115000</td></tr>

<tr><td>British Indian Ocean Territory</td><td>0</td></tr>

<tr><td>Brunei</td><td>328000</td></tr>

<tr><td>Bulgaria</td><td>8190900</td></tr>

<tr><td>Burkina Faso</td><td>11937000</td></tr>

<tr><td>Burundi</td><td>6695000</td></tr>

<tr><td>Cambodia</td><td>11168000</td></tr>

<tr><td>Cameroon</td><td>15085000</td></tr>

<tr><td>Canada</td><td>31147000</td></tr>

<tr><td>Cape Verde</td><td>428000</td></tr>

<tr><td>Cayman Islands</td><td>38000</td></tr>

<tr><td>Central African Republic</td><td>3615000</td></tr>

<tr><td>Chad</td><td>7651000</td></tr>

<tr><td>Chile</td><td>15211000</td></tr>

<tr><td>China</td><td>1277558000</td></tr>

<tr><td>Christmas Island</td><td>2500</td></tr>

<tr><td>Cocos (Keeling) Islands</td><td>600</td></tr>

<tr><td>Colombia</td><td>42321000</td></tr>

<tr><td>Comoros</td><td>578000</td></tr>

<tr><td>Congo</td><td>2943000</td></tr>

<tr><td>Congo, The Democratic Republic of the</td><td>51654000</td></tr>

<tr><td>Cook Islands</td><td>20000</td></tr>

<tr><td>Costa Rica</td><td>4023000</td></tr>

<tr><td>Côte d’Ivoire</td><td>14786000</td></tr>

<tr><td>Croatia</td><td>4473000</td></tr>

<tr><td>Cuba</td><td>11201000</td></tr>

<tr><td>Cyprus</td><td>754700</td></tr>

<tr><td>Czech Republic</td><td>10278100</td></tr>

<tr><td>Denmark</td><td>5330000</td></tr>

<tr><td>Djibouti</td><td>638000</td></tr>

<tr><td>Dominica</td><td>71000</td></tr>

<tr><td>Dominican Republic</td><td>8495000</td></tr>

<tr><td>East Timor</td><td>885000</td></tr>

<tr><td>Ecuador</td><td>12646000</td></tr>

<tr><td>Egypt</td><td>68470000</td></tr>

<tr><td>El Salvador</td><td>6276000</td></tr>

<tr><td>Equatorial Guinea</td><td>453000</td></tr>

<tr><td>Eritrea</td><td>3850000</td></tr>

<tr><td>Estonia</td><td>1439200</td></tr>

<tr><td>Ethiopia</td><td>62565000</td></tr>

<tr><td>Falkland Islands</td><td>2000</td></tr>

<tr><td>Faroe Islands</td><td>43000</td></tr>

<tr><td>Fiji Islands</td><td>817000</td></tr>

<tr><td>Finland</td><td>5171300</td></tr>

<tr><td>France</td><td>59225700</td></tr>

<tr><td>French Guiana</td><td>181000</td></tr>

<tr><td>French Polynesia</td><td>235000</td></tr>

<tr><td>French Southern territories</td><td>0</td></tr>

<tr><td>Gabon</td><td>1226000</td></tr>

<tr><td>Gambia</td><td>1305000</td></tr>

<tr><td>Georgia</td><td>4968000</td></tr>

<tr><td>Germany</td><td>82164700</td></tr>

<tr><td>Ghana</td><td>20212000</td></tr>

<tr><td>Gibraltar</td><td>25000</td></tr>

<tr><td>Greece</td><td>10545700</td></tr>

<tr><td>Greenland</td><td>56000</td></tr>

<tr><td>Grenada</td><td>94000</td></tr>

<tr><td>Guadeloupe</td><td>456000</td></tr>

<tr><td>Guam</td><td>168000</td></tr>

<tr><td>Guatemala</td><td>11385000</td></tr>

<tr><td>Guinea</td><td>7430000</td></tr>

<tr><td>Guinea-Bissau</td><td>1213000</td></tr>

<tr><td>Guyana</td><td>861000</td></tr>

<tr><td>Haiti</td><td>8222000</td></tr>

<tr><td>Heard Island and McDonald Islands</td><td>0</td></tr>

<tr><td>Holy See (Vatican City State)</td><td>1000</td></tr>

<tr><td>Honduras</td><td>6485000</td></tr>

<tr><td>Hong Kong</td><td>6782000</td></tr>

<tr><td>Hungary</td><td>10043200</td></tr>

<tr><td>Iceland</td><td>279000</td></tr>

<tr><td>India</td><td>1013662000</td></tr>

<tr><td>Indonesia</td><td>212107000</td></tr>

<tr><td>Iran</td><td>67702000</td></tr>

<tr><td>Iraq</td><td>23115000</td></tr>

<tr><td>Ireland</td><td>3775100</td></tr>

<tr><td>Israel</td><td>6217000</td></tr>

<tr><td>Italy</td><td>57680000</td></tr>

<tr><td>Jamaica</td><td>2583000</td></tr>

<tr><td>Japan</td><td>126714000</td></tr>

<tr><td>Jordan</td><td>5083000</td></tr>

<tr><td>Kazakstan</td><td>16223000</td></tr>

<tr><td>Kenya</td><td>30080000</td></tr>

<tr><td>Kiribati</td><td>83000</td></tr>

<tr><td>Kuwait</td><td>1972000</td></tr>

<tr><td>Kyrgyzstan</td><td>4699000</td></tr>

<tr><td>Laos</td><td>5433000</td></tr>

<tr><td>Latvia</td><td>2424200</td></tr>

<tr><td>Lebanon</td><td>3282000</td></tr>

<tr><td>Lesotho</td><td>2153000</td></tr>

<tr><td>Liberia</td><td>3154000</td></tr>

<tr><td>Libyan Arab Jamahiriya</td><td>5605000</td></tr>

<tr><td>Liechtenstein</td><td>32300</td></tr>

<tr><td>Lithuania</td><td>3698500</td></tr>

<tr><td>Luxembourg</td><td>435700</td></tr>

<tr><td>Macao</td><td>473000</td></tr>

<tr><td>Macedonia</td><td>2024000</td></tr>

<tr><td>Madagascar</td><td>15942000</td></tr>

<tr><td>Malawi</td><td>10925000</td></tr>

<tr><td>Malaysia</td><td>22244000</td></tr>

<tr><td>Maldives</td><td>286000</td></tr>

<tr><td>Mali</td><td>11234000</td></tr>

<tr><td>Malta</td><td>380200</td></tr>

<tr><td>Marshall Islands</td><td>64000</td></tr>

<tr><td>Martinique</td><td>395000</td></tr>

<tr><td>Mauritania</td><td>2670000</td></tr>

<tr><td>Mauritius</td><td>1158000</td></tr>

<tr><td>Mayotte</td><td>149000</td></tr>

<tr><td>Mexico</td><td>98881000</td></tr>

<tr><td>Micronesia, Federated States of</td><td>119000</td></tr>

<tr><td>Moldova</td><td>4380000</td></tr>

<tr><td>Monaco</td><td>34000</td></tr>

<tr><td>Mongolia</td><td>2662000</td></tr>

<tr><td>Montserrat</td><td>11000</td></tr>

<tr><td>Morocco</td><td>28351000</td></tr>

<tr><td>Mozambique</td><td>19680000</td></tr>

<tr><td>Myanmar</td><td>45611000</td></tr>

<tr><td>Namibia</td><td>1726000</td></tr>

<tr><td>Nauru</td><td>12000</td></tr>

<tr><td>Nepal</td><td>23930000</td></tr>

<tr><td>Netherlands</td><td>15864000</td></tr>

<tr><td>Netherlands Antilles</td><td>217000</td></tr>

<tr><td>New Caledonia</td><td>214000</td></tr>

<tr><td>New Zealand</td><td>3862000</td></tr>

<tr><td>Nicaragua</td><td>5074000</td></tr>

<tr><td>Niger</td><td>10730000</td></tr>

<tr><td>Nigeria</td><td>111506000</td></tr>

<tr><td>Niue</td><td>2000</td></tr>

<tr><td>Norfolk Island</td><td>2000</td></tr>

<tr><td>North Korea</td><td>24039000</td></tr>

<tr><td>Northern Mariana Islands</td><td>78000</td></tr>

<tr><td>Norway</td><td>4478500</td></tr>

<tr><td>Oman</td><td>2542000</td></tr>

<tr><td>Pakistan</td><td>156483000</td></tr>

<tr><td>Palau</td><td>19000</td></tr>

<tr><td>Palestine</td><td>3101000</td></tr>

<tr><td>Panama</td><td>2856000</td></tr>

<tr><td>Papua New Guinea</td><td>4807000</td></tr>

<tr><td>Paraguay</td><td>5496000</td></tr>

<tr><td>Peru</td><td>25662000</td></tr>

<tr><td>Philippines</td><td>75967000</td></tr>

<tr><td>Pitcairn</td><td>50</td></tr>

<tr><td>Poland</td><td>38653600</td></tr>

<tr><td>Portugal</td><td>9997600</td></tr>

<tr><td>Puerto Rico</td><td>3869000</td></tr>

<tr><td>Qatar</td><td>599000</td></tr>

<tr><td>Réunion</td><td>699000</td></tr>

<tr><td>Romania</td><td>22455500</td></tr>

<tr><td>Russian Federation</td><td>146934000</td></tr>

<tr><td>Rwanda</td><td>7733000</td></tr>

<tr><td>Saint Helena</td><td>6000</td></tr>

<tr><td>Saint Kitts and Nevis</td><td>38000</td></tr>

<tr><td>Saint Lucia</td><td>154000</td></tr>

<tr><td>Saint Pierre and Miquelon</td><td>7000</td></tr>

<tr><td>Saint Vincent and the Grenadines</td><td>114000</td></tr>

<tr><td>Samoa</td><td>180000</td></tr>

<tr><td>San Marino</td><td>27000</td></tr>

<tr><td>Sao Tome and Principe</td><td>147000</td></tr>

<tr><td>Saudi Arabia</td><td>21607000</td></tr>

<tr><td>Senegal</td><td>9481000</td></tr>

<tr><td>Seychelles</td><td>77000</td></tr>

<tr><td>Sierra Leone</td><td>4854000</td></tr>

<tr><td>Singapore</td><td>3567000</td></tr>

<tr><td>Slovakia</td><td>5398700</td></tr>

<tr><td>Slovenia</td><td>1987800</td></tr>

<tr><td>Solomon Islands</td><td>444000</td></tr>

<tr><td>Somalia</td><td>10097000</td></tr>

<tr><td>South Africa</td><td>40377000</td></tr>

<tr><td>South Georgia and the South Sandwich Islands</td><td>0</td></tr>

<tr><td>South Korea</td><td>46844000</td></tr>

<tr><td>Spain</td><td>39441700</td></tr>

<tr><td>Sri Lanka</td><td>18827000</td></tr>

<tr><td>Sudan</td><td>29490000</td></tr>

<tr><td>Suriname</td><td>417000</td></tr>

<tr><td>Svalbard and Jan Mayen</td><td>3200</td></tr>

<tr><td>Swaziland</td><td>1008000</td></tr>

<tr><td>Sweden</td><td>8861400</td></tr>

<tr><td>Switzerland</td><td>7160400</td></tr>

<tr><td>Syria</td><td>16125000</td></tr>

<tr><td>Taiwan</td><td>22256000</td></tr>

<tr><td>Tajikistan</td><td>6188000</td></tr>

<tr><td>Tanzania</td><td>33517000</td></tr>

<tr><td>Thailand</td><td>61399000</td></tr>

<tr><td>Togo</td><td>4629000</td></tr>

<tr><td>Tokelau</td><td>2000</td></tr>

<tr><td>Tonga</td><td>99000</td></tr>

<tr><td>Trinidad and Tobago</td><td>1295000</td></tr>

<tr><td>Tunisia</td><td>9586000</td></tr>

<tr><td>Turkey</td><td>66591000</td></tr>

<tr><td>Turkmenistan</td><td>4459000</td></tr>

<tr><td>Turks and Caicos Islands</td><td>17000</td></tr>

<tr><td>Tuvalu</td><td>12000</td></tr>

<tr><td>Uganda</td><td>21778000</td></tr>

<tr><td>Ukraine</td><td>50456000</td></tr>

<tr><td>United Arab Emirates</td><td>2441000</td></tr>

<tr><td>United Kingdom</td><td>59623400</td></tr>

<tr><td>United States</td><td>278357000</td></tr>

<tr><td>United States Minor Outlying Islands</td><td>0</td></tr>

<tr><td>Uruguay</td><td>3337000</td></tr>

<tr><td>Uzbekistan</td><td>24318000</td></tr>

<tr><td>Vanuatu</td><td>190000</td></tr>

<tr><td>Venezuela</td><td>24170000</td></tr>

<tr><td>Vietnam</td><td>79832000</td></tr>

<tr><td>Virgin Islands, British</td><td>21000</td></tr>

<tr><td>Virgin Islands, U.S.</td><td>93000</td></tr>

<tr><td>Wallis and Futuna</td><td>15000</td></tr>

<tr><td>Western Sahara</td><td>293000</td></tr>

<tr><td>Yemen</td><td>18112000</td></tr>

<tr><td>Yugoslavia</td><td>10640000</td></tr>

<tr><td>Zambia</td><td>9169000</td></tr>

<tr><td>Zimbabwe</td><td>11669000</td></tr>

</table>

<pre style="border-top: solid 1px black">Copyright @ 2009 Beda Kosata</pre>
</body>
</html>
Doba běhu: 294.2 ms