简体   繁体   English

使用jinja2仅渲染HTML

[英]Render only HTML using jinja2

I am using Jinja2 to render a html page, which in turn have a Javascript code that generates a div. 我正在使用Jinja2渲染一个html页面,而html页面又有一个生成div的Javascript代码。 I need the html resulting with the executed JavaScript code, not the tag like this: 我需要使用执行的JavaScript代码生成的html,而不是像这样的标记:

<html>
<body>
<script>create_a_div()</script>
</body>
</html>

The desired output is: 所需的输出是:

<html>
<body>
<div></div>
</body>
</html>

Is there any argument I could use in the function render to do this? 我可以在函数渲染中使用任何参数来执行此操作吗?

I would let that JavaScript code to be executed on Client side, but I am not allowed to save it for security reasons. 我会让JavaScript代码在客户端执行,但出于安全原因,我不允许保存它。

If I clearly understand what you are asking for, I'm afraid the answer is no , you cannot execute the JavaScript code in render function. 如果我清楚地理解你的要求,我恐怕答案是否定的 ,你不能在渲染函数中执行JavaScript代码。

What you could do, is pass the html as a variable into the template with the safe filter. 您可以做的是将html作为变量传递到带有安全过滤器的模板中

Template: 模板:

<html>
<body>
  {{ custom_html|safe }}
</body>
</html>

And when rendering, use the following Python code: 在渲染时,使用以下Python代码:

template.render(custom_html="<div>your html code</div>") 

This would give you the desired output that you are looking for. 这将为您提供所需的输出。

You can read more about JINJA2 templates and variables here 您可以在此处阅读有关JINJA2模板和变量的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM