HTML templating using Jinja2 No module named your app

谁说我不能喝 提交于 2019-12-21 09:26:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!