[英]Generating an HTML code from a flask server
I have a pythonic Flask server with a list that has some elements. 我有一个pythonic Flask服务器,列表中有一些元素。 This list has some entries.
此列表包含一些条目。 I want to produce a html page with the elements of the list.
我想生成一个包含列表元素的html页面。
Note: I am building a app where this list has all the things that the user shopped and then at the end we need to show the list of shopped things. 注意:我正在构建一个应用程序,其中此列表包含用户购买的所有内容,然后最后我们需要显示购物内容列表。 I stored in the list using request.form().
我使用request.form()存储在列表中。
You will want to use flask.render_template
to do this. 您将需要使用
flask.render_template
来执行此操作。
That function takes one positional argument and as many keyword arguments as you want. 该函数根据需要采用一个位置参数和多个关键字参数。 Use it like this.
像这样使用它。
from Flask import render_template
@yourApp.route('/your/path')
def renderThisPath:
res = render_template('your-jinja2-template-file.html',
some='variables',
you='want',
toPass=['to','your','template'])
return res
then your template you'd do this: 然后你的模板你这样做:
<html>
<!--head etc-->
<body>
<div>{{ you }}</div>
<div>{{ some }}<span>?</span></div>
<!-- you can iterate on a list to add many items -->
{% for v in toPass %}
<div>{{ v }}</div>
{% endfor %}
</body>
</html>
renders: 呈现:
<html>
<!--head etc-->
<body>
<div>want</div>
<div>variables<span>?</span></div>
<!-- you can iterate on a list to add many items -->
<div>to</div>
<div>your</div>
<div>template</div>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.