views:

714

answers:

3

As the title suggests, I'm using Google App Engine and Django.

I have quite a bit of identical code across my templates and would like to reduce this by including template files. So, in my main application directory I have the python handler file, the main template, and the template I want to include in my main template.

I would have thought that including {% include "fileToInclude.html" %} would work on its own but that simply doesn't include anything. I assume I have to set something up, maybe using TEMPLATE_DIRS, but can't figure it out on my own.

EDIT:

I've tried:

TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), 'templates'), )

But to no avail. I'll try some other possibilities too.

+3  A: 

First, you should consider using template inheritance rather than the include tag, which is often appropriate but sometimes far inferior to template inheritance.

Unfortunately, I have no experience with App Engine, but from my experience with regular Django, I can tell you that you need to set your TEMPLATE_DIRS list to include the folder from which you want to include a template, as you indicated.

Eli Courtwright
+1  A: 

I found that it works "out of the box" if I don't load Templates first and render them with a Context object. Instead, I use the standard method shown in the AppEngine tutorial.

A: 

I've done the following to get around using includes:

def render(file, map={}):
  return template.render(
    os.path.join(os.path.dirname(__file__), '../templates', file), map)  

table = render("table.html", {"headers": headers, "rows": rows})   
final = render("final.html", {"table": table})

self.response.out.write(final)
masher