Step 10: Template Payload

A repackaged payload of List and Item data is an argument to the template renderer, to compensate for limitations in the Django system.

We need to display the list itself. I already have an idea for displaying items, so I'll write the code for rendering in one try.

Which brings us to the biggest consequence of the Django template philosophy, at this stage -- our function render needs to pre-package a new structure, to pass to the Django template rendering system. This is used by the "{% for list in lists %}" and "{% for item in list.items %}" template tags in my index.html.

To do this, render needs to fetch Lists and Items from the datastore and create a kind of populating payload, which I build as "new_lists" below, and rename as "lists" to pass to the template, within the "template_values" dict structure:

def render (template_values):

lists = db.GqlQuery("SELECT * FROM List")

new_lists = []

for list in lists:

items = db.GqlQuery("SELECT * FROM Item Where list_key = :1", list.key())

new_list = {

'name': list.name,

'key': str(list.key()),

'items': items }

new_lists.append(new_list)

template_values['lists'] = new_lists

path = os.path.join(os.path.dirname(__file__), 'index.html')

return(template.render(path, template_values))

... which is unraveled by the logic in the index.html Django template.

(Note the need to cast the list.key() to a string for the payload).