hqt hqt - 3 months ago 16
Python Question

App Engine : sub folder in templates

I'm using

template
to separate HTML and Python Code. I create templates folder. in this folder, I create index.html. Here is my code :

template_dir = os.path.join(os.path.dirname(__file__),'templates')
jinja_environment = jinja2.Environment(autoescape=True, loader=jinja2.FileSystemLoader(template_dir))

def render(self, template, **params):
t = jinja_environment.get_template(template)
return t.render(params)


And using render('index.html') : successful, no problem.

But when I create sub folder : templates/blog and in this folder, I create blog.html. I change the template above from
templates
to
templates/blog
. Here is my code :

template_dir = os.path.join(os.path.dirname(__file__),'templates/blog')
jinja_environment = jinja2.Environment(autoescape=True, loader=jinja2.FileSystemLoader(template_dir))


but, when I use render('blog.html') I have received error :
TemplateNotFound: blog.html
.

Please help me.

Thanks :)

Answer

You can add more folders in jinja2 enviroment:

jinja_environment = jinja2.Environment(
  loader=jinja2.FileSystemLoader(['templates', 'templates\blog'])
)