HTML templates using Jinja2 No module named for your application

I am trying to create an html template in python using Jinja2. I have a templates folder with my "template.html", but I don’t know how to handle environments or package loaders.

I installed Jinja2. These are my simple codes

from jinja2 import Environment, PackageLoader env = Environment(loader=PackageLoader('ap', 'templates')) template = env.get_template('template.html') print template.render(title='hello') 

I get this error:

 File "a.py", line 3, in <module> env = Environment(loader=PackageLoader('ap', 'templates')) File "/usr/local/lib/python2.7/dist-packages/Jinja2-2.7-py2.7.egg/jinja2/loaders.py", line 214, in __init__ provider = get_provider(package_name) File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 213, in get_provider __import__(moduleOrReq) ImportError: No module named ap 

These are my folders.

 ap/ __init__.py a.py templates/ template.html 

Where am I mistaken? Why am I getting this error "No module named your application"

+7
source share
1 answer

I don't know much about environments and bootloaders, but this is what I use:

 jinja_environment = jinja2.Environment( loader=jinja2.FileSystemLoader('%s/templates/' % os.path.dirname(__file__)) ) 
+15
source

Source: https://habr.com/ru/post/1493419/


All Articles