问题
I am trying to create a html template in python using Jinja2. I have a templates folder with my 'template.html' but I don't know how to deal with environments or package loaders.
I installed Jinja2. These 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
This my folders
ap/
__init__.py
a.py
templates/
template.html
Where am I wrong ? Why I get this error "No module named your application"
回答1:
I don't know much about Environments and Loaders, but this is what I use:
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader('%s/templates/' % os.path.dirname(__file__))
)
来源:https://stackoverflow.com/questions/17855933/html-templating-using-jinja2-no-module-named-your-app