flash 烟花模板_Flask模板引擎中的For循环

news/2024/7/19 11:29:32 标签: 列表, python, java, vue, web

flash 烟花模板

Flask comes with a Jinja templating language. The template essentially contains variables as well as some programming logic, which when evaluated are rendered into HTML with actual values. The variables and/or logic are placed between tags or delimiters.

Flask带有Jinja模板语言 。 该模板实质上包含变量以及一些编程逻辑,这些逻辑在进行评估时会以实际值呈现为HTML。 变量和/或逻辑放置在标签或定界符之间。

Jinja templates are HTML files which by flask conventions are placed in the templates folder in the Flask project.

Jinja模板是HTML文件,根据烧瓶惯例,它们位于Flask项目的template文件夹中。

Having a loop of the list of users or any list as a matter is a very obvious requirement in a web application. It would be very naïve and unmanageable to add a new page for every new item in the list. Here is where the for-loop in the jinja templates are useful.

Web应用程序中非常明显的要求是拥有用户列表或任何列表循环 。 为列表中的每个新项目添加一个新页面将非常幼稚且难以管理。 这是Jinja模板中的for循环有用的地方。

Consider the following example,

考虑以下示例,

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/jinja")
def jinja_test():
    return render_template('include_help.html', my_string="Include Help!", my_list=[0,1,2,3,4,5])
  
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5005)

In the above example, when in the browser the user navigates to http://localhost:5005/jinja the rendered web page will sequentially display the list number.

在上面的示例中,当用户在浏览器中导航到http:// localhost:5005 / jinja时 ,呈现的网页将顺序显示列表号。

The list could as simple as a list of numbers or users or profiles to make it complex.

列表可能像数字列表,用户列表或个人资料列表一样简单,从而使其变得复杂。

The 'include_help.html' is implemented as

“ include_help.html”的实现方式为

<html>

<head>
    <title>Include help Template Example</title>
</head>

<body>
    <p>{{my_string}}</p>
    <p>Your lucky number is ;) : {{my_list[3]}}</p>
    <p>Loop through the list:</p>
    <ul>
        {% for n in my_list %}
        <li>{{n}}</li>
        {% endfor %}
    </ul>
</body>

</html>

On rendering of the template, the my_string and my_list is replaced with the actual values sent from the backend code. The my_list is then iterated using a for loop. The for loop starts with a for and ends with endfor inside the {%%} tag.

呈现模板时, my_string和my_list替换为从后端代码发送的实际值。 然后使用for循环迭代my_list 。 for循环以{%%}标记内的for开头,以endfor结尾。

On running the above code, the html rendered is,

运行以上代码后,呈现的html是,

for loop in Python flask

翻译自: https://www.includehelp.com/python/for-loop-in-flask-template-engine.aspx

flash 烟花模板


http://www.niftyadmin.cn/n/1254118.html

相关文章

如何PHP给人生日祝福,简短12字 最打动人的生日祝福语

1、今天是你的生日&#xff0c;能成为你的朋友&#xff0c;是我一生中最幸运的事&#xff0c;祝你生日快乐&#xff0c;事事如意。2、这是我的真诚&#xff0c;祝你幸福直到永远&#xff0c;幸福&#xff0c;平安&#xff01;3、今天是你的生日&#xff0c;我的朋友&#xff0c…

ruby 自定义排序_在Ruby中创建自定义错误记录器

ruby 自定义排序We have gone through Exception handling in Ruby. We can handle exceptions with the help of begin...rescue block. Handling Exceptions or Errors is really because if you dont do so, you would not be able to process the rest of the code lines. …

php怎么让数组转换为字符串,php如何将数组转换为字符串

【摘要】PHP即“超文本预处理器”&#xff0c;是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言&#xff0c;与C语言类似&#xff0c;是常用的网站编程语言。PHP独特的语法混合了C、Java、Perl以及 PHP 自创的语法。下面是php如何将数组转换为字符串&#xff0c;让我们一…

什么是bcd码数据传输通讯_数据传输 数据通讯

什么是bcd码数据传输通讯资料传输 (Data Transmission) When considering the transmission of data from one device to another is the wiring, and of primary concern when considering the wiring is the data stream. 在考虑将数据从一个设备传输到另一设备时&#xff0c…

python 向量二范数_向量的范数| 使用Python的线性代数

python 向量二范数Prerequisite: 先决条件&#xff1a; Defining Vector using Numpy 使用Numpy定义向量 Here, we are going to learn how to find the norm of a vector using an inbuilt function in numpy library? 在这里&#xff0c;我们将学习如何使用numpy库中的内置…

php select含义,将SQL查询的SELECT子句解析为PHP数组

这更适用于在PHP发送到服务器之前分析PHP中的查询.非常复杂为什么我这样做,所以我宁愿不去理解这个.在PHP中,我需要将字段选择存储到php数组中.所以以此查询为例&#xff1a;SELECT user_id,username,DATE(join_datetime) as join_date, (SELECT COUNT(1) FROM foobar WHERE fo…

Java多线程(四)java中的Sleep方法

点我跳过黑哥的卑鄙广告行为&#xff0c;进入正文。 Java多线程系列更新中~ 正式篇&#xff1a; Java多线程&#xff08;一&#xff09; 什么是线程Java多线程&#xff08;二&#xff09;关于多线程的CPU密集型和IO密集型这件事Java多线程&#xff08;三&#xff09;如何创建线…

javascript子节点_JavaScript中的传播算子

javascript子节点JavaScript传播运算符 (JavaScript Spread Operator) The spread operator came out in ES6 and has great functionality for arrays in JavaScript. With a major update in ES9, the spread operator is now extended to objects in addition to arrays. In…