SubControllers And The URL Hierarchy - jordy33/turbogears_tutorial GitHub Wiki
Sometimes your web-app needs a URL structure that’s more than one level deep.
TurboGears provides for this by traversing the object hierarchy, to find a method that can handle your request.
To make a sub-controller, all you need to do is make your sub-controller inherit from the object class.
Create a folder inside templates and name it statistics. Inside the statistics folder create __.init__.py and insert the following:
# -*- coding: utf-8 -*-
Inside the statistics folder create population.mak and insert the following:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=1024">
<title>Example 01: No CSS</title>
</head>
<body>
<div id="wrapper">
<div class="chart">
<h3>Population of endangered species from 2012 to 2016</h3>
<table id="data-table" border="1" cellpadding="10" cellspacing="0"
summary="The effects of the zombie outbreak on the populations
of endangered species from 2012 to 2016">
<caption>Population in thousands</caption>
<thead>
<tr>
<td> </td>
<th scope="col">2012</th>
<th scope="col">2013</th>
<th scope="col">2014</th>
<th scope="col">2015</th>
<th scope="col">2016</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Carbon Tiger</th>
<td>4080</td>
<td>6080</td>
<td>6240</td>
<td>3520</td>
<td>2240</td>
</tr>
<tr>
<th scope="row">Blue Monkey</th>
<td>5680</td>
<td>6880</td>
<td>6240</td>
<td>5120</td>
<td>2640</td>
</tr>
<tr>
<th scope="row">Tanned Zombie</th>
<td>1040</td>
<td>1760</td>
<td>2880</td>
<td>4720</td>
<td>7520</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
Create statistics.py inside controllers folder an insert the following:
# -*- coding: utf-8 -*-
"""The base Controller API."""
from myprojectname.lib.base import BaseController
from tg import expose
__all__ = ['statisticsController']
class StatisticsController(BaseController):
@expose('myprojectname.templates.statistics.population')
def population(self):
return dict()
Inside root.py at the top insert the following
from myprojectname.controllers.statistics import StatisticsController
Find this code
error = ErrorController()
Bellow insert the following code
statistics = StatisticsController()
Test the code:
http://localhost:8080/statistics/population