AppEngine and Django: including a template file

As the name implies, I use the Google App Engine and Django.

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

I would think that the inclusion of {% include "fileToInclude.html"%} would work on its own, but it just does not contain anything. I assume that I need to change something, perhaps using TEMPLATE_DIRS, but I can not figure it out on my own.

EDIT:

I tried:

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

But to no avail. I will also try other possibilities.

+4
source share
4 answers

I found that it works out of the box unless I first load the templates and create them using the Context object. Instead, I use the standard method shown in the AppEngine Tutorial .

+1
source

First, you should use 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 say that you need to set the list from the TEMPLATE_DIRS list from which you want to include the template, as you specified.

+3
source

I had the same problem and tracked it in the ext.webapp package. In template.py you will find this comment on line 33:

Django uses a global parameter for the directory in which it searches for templates. This is not natural in the context of the webapp module, so our download method takes the full path of the template, and we set these settings on the fly automatically. Since we must set and use global settings on each method, this module is not thread safe, although this is not a problem for applications.

See line 92 in the same file. You can see how dirs templates are compressed:

 directory, file_name = os.path.split(abspath) new_settings = { 'TEMPLATE_DIRS': (directory,), 'TEMPLATE_DEBUG': debug, 'DEBUG': debug, } 

UPDATE: here is the workaround that worked for me - http://groups.google.com/group/google-appengine/browse_thread/thread/c3e0e4c47e4f3680/262b517a723454b6?lnk=gst&q=template_dirs#262b517a723454b6

+1
source

I did the following to get around, including:

 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) 
0
source

All Articles