【问题标题】:How do I render jinja2 output to a file in Python instead of a Browser如何将 jinja2 输出渲染到 Python 中的文件而不是浏览器
【发布时间】:2012-08-05 03:36:51
【问题描述】:

我有一个要渲染的 jinja2 模板(.html 文件)(用我的 py 文件中的值替换标记)。但是,我不想将渲染结果发送到浏览器,而是将其写入新的 .html 文件。我想对于 django 模板来说,解决方案也是类似的。

我该怎么做?

【问题讨论】:

    标签: python django jinja2


    【解决方案1】:

    这样的事情怎么样?

    from jinja2 import Environment, FileSystemLoader
    env = Environment(loader=FileSystemLoader('templates'))
    template = env.get_template('test.html')
    output_from_parsed_template = template.render(foo='Hello World!')
    print(output_from_parsed_template)
    
    # to save the results
    with open("my_new_file.html", "w") as fh:
        fh.write(output_from_parsed_template)
    

    test.html

    <h1>{{ foo }}</h1>
    

    输出

    <h1>Hello World!</h1>
    

    如果您使用的是 Flask 等框架,则可以在返回之前在视图底部执行此操作。

    output_from_parsed_template = render_template('test.html', foo="Hello World!")
    with open("some_new_file.html", "wb") as f:
        f.write(output_from_parsed_template)
    return output_from_parsed_template
    

    【讨论】:

    • 感谢您的快速响应。如果我理解正确,那么到你的第一个片段: from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader('templates')) template = env.get_template('test.html') output_from_parsed_template = template.render(foo ='Hello World!') print output_from_parsed_template 我可以用某种文件写入行替换打印行。那是对的吗?写入文件的这种行可能是什么样的?回覆。 Flask,这是一个较大应用程序的一小部分,所以我不知道我是否可以使用框架。
    • 感谢您的澄清。我终于有机会试试这个了。最初我收到一个错误“没有这样的文件或目录:'my_new_file.html'”。显然该文件已经存在。然后我复制了模板文件并将其重命名为“my_new_file.html”。现在我收到一个错误:IOError: File not open for writing。这可能是因为我正在使用 Google App Engine 进行开发吗?
    • @BillG。不,这是我的错误。试试上面的改变:把rb改成wb
    • 感谢您的快速回复。我将 rb 更改为 wb,现在我收到以下错误:IOError: invalid mode: wb
    • 底部代码部分第一行末尾缺少)。我试图添加它,但 SO 要求编辑大于 6 个字符(愚蠢的限制)..
    【解决方案2】:

    所以在你加载了模板之后,你调用 render 然后将输出写入一个文件。 'with' 语句是一个上下文管理器。在缩进中,您有一个打开的文件,例如名为“f”的对象。

    template = jinja_environment.get_template('CommentCreate.html')     
    output = template.render(template_values)) 
    
    with open('my_new_html_file.html', 'w') as f:
        f.write(output)
    

    【讨论】:

    • 那么,这看起来像:TEMPLATE_DIR = os.path.join(os.path.dirname(file), 'templates') jinja_environment = \ jinja2.Environment (autoescape=False, loader=jinja2.FileSystemLoader(TEMPLATE_DIR)) template = jinja_environment.get_template('CommentCreate.html') self.response.out.write(template.render(template_values)) with open('my_new_html_file.html', 'w') as f: f.write(response.content) where template_values 已经填充。请根据需要进行更正。谢谢。
    • 感谢您的澄清。我终于有机会试试这个了。最初我收到一个错误“没有这样的文件或目录:'my_new_file.html'”。显然该文件已经存在。然后我复制了模板文件并将其重命名为“my_new_file.html”。现在我收到一个错误:IOError: File not open for writing。这可能是因为我正在使用 Google App Engine 进行开发吗?
    【解决方案3】:

    您可以将模板流转储到文件中,如下所示:

    Template('Hello {{ name }}!').stream(name='foo').dump('hello.html')
    

    参考:http://jinja.pocoo.org/docs/dev/api/#jinja2.environment.TemplateStream.dump

    【讨论】:

      猜你喜欢
      • 2017-03-21
      • 2019-08-29
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 2019-10-28
      • 2016-07-05
      • 2015-08-04
      • 2019-03-11
      相关资源
      最近更新 更多