Python Flask框架 -- 控制语句

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/control')
def control_statement():
    age = 19
    books = [{
        'name': '三国演义',
        'author': '罗贯中'
    }, {
        'name': '水浒传',
        'author': '施耐庵'
    }]
    return render_template('control.html', age=age, books=books)

if __name__ == '__main__':
    app.run()

在 templates 中创建 control.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>控制语句demo</title>
</head>
<body>
{% if age>18 %}
    <div>你已经满18岁</div>
{% elif age == 18 %}
    <div>你刚满18岁</div>
{% else %}
    <div>你未满18岁</div>
{% endif %} <!-- Jinja2中使用if开头,就一定要用endif做结尾 -->

{% for book in books %}
    <div>图书名称:{{ book.name }},图书作者:{{ book.author }}</div>
{% endfor %} <!-- Jinja2中使用for开头,就一定要用endfor做结尾 -->
</body>
</html>

 

相关推荐

  1. Shiro框架权限控制

    2024-03-24 07:36:03       34 阅读
  2. 学习Go语言Web框架Gee总结--分组控制Group(四)

    2024-03-24 07:36:03       35 阅读
  3. js流程控制语句

    2024-03-24 07:36:03       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-24 07:36:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-24 07:36:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-24 07:36:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-24 07:36:03       18 阅读

热门阅读

  1. 正则表达式

    2024-03-24 07:36:03       17 阅读
  2. Linux: network:interrupt: python tool

    2024-03-24 07:36:03       16 阅读
  3. C# 编程语言中访问修饰符(access modifiers)

    2024-03-24 07:36:03       17 阅读
  4. SpringBoot 定时器@Scheduled的使用

    2024-03-24 07:36:03       15 阅读
  5. Wpf-自定义控件波纹Button

    2024-03-24 07:36:03       20 阅读
  6. 【大厂面试题购物车】通关代码

    2024-03-24 07:36:03       17 阅读
  7. C++常用的库

    2024-03-24 07:36:03       17 阅读
  8. 基于FPGA的UDP协议栈设计第六章_仲裁模块设计

    2024-03-24 07:36:03       16 阅读
  9. leetcode - 362. Design Hit Counter

    2024-03-24 07:36:03       18 阅读