Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja can't find template path

I can't get Jinja2 to read my template file.

jinja2.exceptions.TemplateNotFound: template.html

The simplest way to configure Jinja2 to load templates for your application looks roughly like this:

from jinja2 import Environment, PackageLoader env = Environment(loader=PackageLoader('yourapplication', 'templates')) This will create a template environment with the default settings and a loader that looks up the templates in the templates folder inside the yourapplication python package. Different loaders are available and you can also write your own if you want to load templates from a database or other resources.

To load a template from this environment you just have to call the get_template() method which then returns the loaded Template:

template = env.get_template('mytemplate.html')

env = Environment(loader=FileSystemLoader('frontdesk', 'templates'))
template = env.get_template('template.html')

My tree ( I have activated the venv @frontdesk )

.
├── classes.py
├── labels.txt
├── payments.py
├── templates
├── test.py
└── venv
like image 380
xavier Avatar asked Mar 04 '16 17:03

xavier


Video Answer


1 Answers

You are using the FileSystemLoader class which has the following init arguments:

class FileSystemLoader(BaseLoader):
    def __init__(self, searchpath, encoding='utf-8', followlinks=False):

You are initializing it with 2 arguments: frontdesk and templates, which basically does not make much sense, since the templates string would be passed as an encoding argument value. If you want to continue using FileSystemLoader as a template loader, use it this way:

from jinja2 import Environment, FileSystemLoader

env = Environment(loader=FileSystemLoader('frontdesk/templates'))
template = env.get_template('index.html')

Or, if you meant to use the PackageLoader class:

from jinja2 import Environment, PackageLoader

env = Environment(loader=PackageLoader('frontdesk', 'templates'))
template = env.get_template('index.html')

In this case you need to make sure frontdesk is a package - in other words, make sure you have __init__.py file inside the frontdesk directory.

like image 142
alecxe Avatar answered Oct 08 '22 07:10

alecxe