Python 使用Jinja2的HTML模板-丢失

Python 使用Jinja2的HTML模板-丢失,python,html,templates,jinja2,Python,Html,Templates,Jinja2,我正在尝试使用Jinja2在python中创建一个html模板。我有一个带有“template.html”的templates文件夹,但我不知道如何处理环境或包加载器 我使用easy_python安装了Jinja2,并运行了以下脚本 from jinja2 import Environment, PackageLoader env = Environment(loader=PackageLoader('yourapplication', 'templates')) template = env.

我正在尝试使用Jinja2在python中创建一个html模板。我有一个带有“template.html”的templates文件夹,但我不知道如何处理环境或包加载器

我使用easy_python安装了Jinja2,并运行了以下脚本

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('yourapplication', 'templates'))
template = env.get_template('mytemplate.html')
print template.render()
我得到以下错误,因为我不知道如何定义包/模块。请帮助我,我只想创建一个简单的模板

  File "log_manipulationLL.py", line 291, in <module>
env = Environment(loader=PackageLoader('yourapplication', 'templates'))
 File "/usr/local/lib/python2.7/dist-packages/Jinja2-2.6-py2.7.egg/jinja2/loaders.py",    line 216, 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 yourapplication
文件“log\u operationll.py”,第291行,在
env=Environment(loader=PackageLoader(“您的应用程序”、“模板”))
文件“/usr/local/lib/python2.7/dist packages/Jinja2-2.6-py2.7.egg/Jinja2/loaders.py”,第216行,在__
provider=get\u provider(包名称)
文件“/usr/lib/python2.7/dist packages/pkg_resources.py”,第213行,在get_provider中
__导入(模块错误)
ImportError:没有名为yourapplication的模块

PackageLoader
需要使用常规点语法的实际Python模块。例如,如果您的结构如下所示:

myapp/
  __init__.py
  …
  templates/
    mytemplate.html
from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
    loader=FileSystemLoader('file/path/'),
    autoescape=select_autoescape(['html', 'xml']),
)
class PackageLoader(BaseLoader):
    """Load templates from python eggs or packages.  It is constructed with
    the name of the python package and the path to the templates in that
    package::

        loader = PackageLoader('mypackage', 'views')

    If the package path is not given, ``'templates'`` is assumed.

    Per default the template encoding is ``'utf-8'`` which can be changed
    by setting the `encoding` parameter to something else.  Due to the nature
    of eggs it's only possible to reload templates if the package was loaded
    from the file system and not a zip file.
    """
myapp/
  __init__.py
  ...
  templates/
    mytemplate.html

您应该使用
myapp
作为模块名称。

我使用以下代码解决了此问题:

 env = Environment(loader=PackageLoader('scriptname', 
                                        templatesPath))
其中,此代码位于文件
scriptname.py


我不确定我的答案是否相关,但我想也许有人会觉得这个答案有用。如果我错了,请告诉我。

如果您不想要或不需要Python软件包,您可能应该使用,例如:

myapp/
  __init__.py
  …
  templates/
    mytemplate.html
from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
    loader=FileSystemLoader('file/path/'),
    autoescape=select_autoescape(['html', 'xml']),
)
class PackageLoader(BaseLoader):
    """Load templates from python eggs or packages.  It is constructed with
    the name of the python package and the path to the templates in that
    package::

        loader = PackageLoader('mypackage', 'views')

    If the package path is not given, ``'templates'`` is assumed.

    Per default the template encoding is ``'utf-8'`` which can be changed
    by setting the `encoding` parameter to something else.  Due to the nature
    of eggs it's only possible to reload templates if the package was loaded
    from the file system and not a zip file.
    """
myapp/
  __init__.py
  ...
  templates/
    mytemplate.html

PackageLoader的定义如下:

myapp/
  __init__.py
  …
  templates/
    mytemplate.html
from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
    loader=FileSystemLoader('file/path/'),
    autoescape=select_autoescape(['html', 'xml']),
)
class PackageLoader(BaseLoader):
    """Load templates from python eggs or packages.  It is constructed with
    the name of the python package and the path to the templates in that
    package::

        loader = PackageLoader('mypackage', 'views')

    If the package path is not given, ``'templates'`` is assumed.

    Per default the template encoding is ``'utf-8'`` which can be changed
    by setting the `encoding` parameter to something else.  Due to the nature
    of eggs it's only possible to reload templates if the package was loaded
    from the file system and not a zip file.
    """
myapp/
  __init__.py
  ...
  templates/
    mytemplate.html
然后
\uuuu init\uuuu()
方法如下:

def __init__(self, package_name, package_path='templates',
             encoding='utf-8'):
这让我们注意到这样的结构:

myapp/
  __init__.py
  …
  templates/
    mytemplate.html
from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
    loader=FileSystemLoader('file/path/'),
    autoescape=select_autoescape(['html', 'xml']),
)
class PackageLoader(BaseLoader):
    """Load templates from python eggs or packages.  It is constructed with
    the name of the python package and the path to the templates in that
    package::

        loader = PackageLoader('mypackage', 'views')

    If the package path is not given, ``'templates'`` is assumed.

    Per default the template encoding is ``'utf-8'`` which can be changed
    by setting the `encoding` parameter to something else.  Due to the nature
    of eggs it's only possible to reload templates if the package was loaded
    from the file system and not a zip file.
    """
myapp/
  __init__.py
  ...
  templates/
    mytemplate.html
将具有与这两个声明相同的
PackageLoader
实例:

PackageLoader('myapp')
PackageLoader('myapp', 'templates')
因此,如果您是从
myapp/
路径运行,那么您只需要说:

PackageLoader('templates', '')
因此,它将只使用
模板/
作为路径。如果将第二个参数留空,它将尝试在
模板/templates
中查找模板

最后,您可以使用
list\u templates()
方法检查已加载的内容:

PackageLoader('templates', '').list_templates()

包加载器调用
scriptname.py
。如果您在其中初始化包加载器,代码将被再次调用。您必须小心-如果您的代码不是全部在函数/类中,重新导入同一文件可能会重新执行其中的代码,从而导致双模板输出!