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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With