Search code examples
pythontemplatestornado

How can I extend a tornado template from a parent template by giving a path starting from template_path?


I set the template_path configuration variable to the templates folder from my project.

In this folder I have the following templates:

base.html
admin/register.html

I want register.html to extend base.html so I have:

{% extend "base.html" %}

This results in the following error:

IOError: [Errno 2] No such file or directory: '/home/kj/Lab/test/templates/admin/base.html'

Apparently it's looking at the same directory with the child template. Of course I can reach base.html with ../base.html but I think it's much better if I can give it an "absolute path" (in the sense of starting from template_path)

Anyone can help?


Solution

  • The way render works, I don't think it's possible. When you pass the name of template to render function, like:

    RequestHandler.render('admin/register.html')
    

    render will start looking for the template from template_path/admin directory. extends which is an operator, searches the template in the admin directory, so you have to give relative path. I will update this answer if I find anything more.

    EDIT: I have confirmed that this is not possible as of now and it has been added as an feature request on Tornado's issue tracker by Ben Darnell (he maintains Tornado project).