【问题标题】:First Jinja not running第一个 Jinja 没有运行
【发布时间】:2021-01-18 09:39:53
【问题描述】:

我是第一次运行 Python Jinja 模板文件,但是通过在线查找我没有找到明确的示例。

下面是我的代码,first.py 是我的 Python 应用程序,模板输出是 home.html

这是error log Image

    from jinja2 import Environment, PackageLoader, select_autoescape
    env = Environment(loader=PackageLoader('first.py', 'templates'),
    autoescape=select_autoescape(['html', 'xml'])
    template = env.get_template('home.html')
    print template.render(the='variables', go='here')

我缺少什么让它运行?渲染中应该包含什么变量?

【问题讨论】:

  • 错误日志是什么?
  • 我在图片上方添​​加了错误日志

标签: python jinja2


【解决方案1】:

您忘记关闭第 3 行的括号

from jinja2 import Environment, PackageLoader, select_autoescape

env = Environment(
    loader=PackageLoader('first.py', 'templates'),
    autoescape=select_autoescape(['html', 'xml']))  # <- you forgot ) here
template = env.get_template('home.html')
print template.render(the='variables', go='here')

要避免这种明显的错误,请使用适当的 IDE。例如PyCharm,它具有错误检查功能。

【讨论】:

  • 我仍然面临同样的错误,我不知道可能是什么问题
猜你喜欢
  • 2021-10-16
  • 2012-11-17
  • 2021-05-27
  • 2021-08-19
  • 1970-01-01
  • 2019-10-05
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多