惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
GbyAI
GbyAI
aimingoo的专栏
aimingoo的专栏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
腾讯CDC
博客园 - Franky
Engineering at Meta
Engineering at Meta
C
Check Point Blog
T
The Blog of Author Tim Ferriss
有赞技术团队
有赞技术团队
Microsoft Azure Blog
Microsoft Azure Blog
MyScale Blog
MyScale Blog
I
InfoQ
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
Last Week in AI
Last Week in AI
S
SegmentFault 最新的问题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学

Hi, I Am I

[I Am I 年度简报] — 不知终日梦为鱼 初探 ESP32-CAM QQ 聊天记录 MHT 文件转 HTML [I Am I 年度简报] - 草木本无意,荣枯自有时。 Hexo 中实现 Live Photos 支持 写在当下 NKCTF 2024 1z_F0r3ns1c5 Writeup 春秋杯冬季赛 2023 Writeup [I Am I 年度简报] - 2023 某内网渗透内部赛 Writeup 强网拟态 2023 Writeup Github Actions 自动化部署 Hexo 浅析CobaltStrike流量解密 陇剑杯 2023 Writeup CTF线下赛AWDP总结 ISCC 2023 Writeup ISCC 2023 实战题 Writeup CISCN 2023 Writeup 福建闽盾杯网络空间安全大赛 2023 Writeup 天一永安杯宁波市网络安全大赛 2023 Writeup 贵阳大数据及网络安全精英对抗赛 2023 Writeup 红明谷杯 2023 Writeup Confetti 带来有仪式感的鼓励 记一次 JS 逆向密码加密 [I Am I 年度简报] – 2022 PHP 读取 Excel 文件内容并写入数据库 从0开始的 MoeCTF 开发之路 观安杯 2022 Writeup 利用微信服务号实现早安自动化 Cloudflare批量拉黑IP脚本
Flask 框架学习记录
2021-08-21 · via Hi, I Am I

Flask是一个使用Python编写的轻量级Web应用框架。基于Werkzeug WSGI工具箱和Jinja2模板引擎。

主要是为了写 edusrc用户信息统计脚本 写的很浅显,堪堪入门吧…

安装

pip install flask

初始化

先导入一个 Flask 类的对象,并创建一个该类的实例

from flask import Flask
app = Flask(__name__)

路由

路由就不用多说了,用来把为用户请求的 URL 找出其对应的 视图函数

@app.route('/')
def index():
    return 'Hello,flask!'

当我们访问主页 / 的时候,页面就会自动调用 index() 函数,显示 Hello,flask!

路径传参

格式:url/参数,然后再 视图函数 中接收参数

@app.route('/')
def index():
    return 'Hello,flask!'

@app.route('/<username>')
def name(username):
    return 'Hello,' + str(username)

这样访问 url/iami233 的时候页面就会显示 Hello,iami233,当然我们也可以限制 <username> 的传入类型(<int:userid>)。

  • string(缺省值): 接受任何 不包含斜杠的文本
  • int : 接受 正整数
  • float : 接受 正浮点数
  • path : 类似 string,但可以包含斜杠
  • uuid : 接受 UUID 字符串

HTTP方法

默认情况下路由只响应 GET 请求。 不过可以使用 route() 装饰器的 methods 参数来处理不同的 HTTP 方法。

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        return do_the_login()
    else:
        return show_the_login_form()

渲染模板

使用 render_template() 方法可以渲染模板,我们只要提供 模板名称 和需要作为 参数 传递给模板的 变量 即可。

from flask import render_template
# 注意我们要导入render_template

@app.route('/hello/<username>')
def hello(username):
    return render_template('hello.html',
        username = username
    )

html 模板需要放在 templates 目录中

/main.py
/templates
    /hello.html

模板示例

<!doctype html>
<title>Hello Flask</title>
<h1>Hello, {{username}}</h1>

jinja2

Flask 使用 Jinja 2 作为 模板引擎,在 Jinja 2 中,存在三种语法

{% %} 控制结构
{{ }} 变量取值
{# #}

一些博主常用操作

# 循环输出变量data
{% for i in data %}
    ...
{% endfor %}

# 判断变量data的长度是否小于五
# 如果小于则根据实际长度与5的差值进行循环
{% if data | length < 5 %}
    {% for i in range(5 - data | length) %}
        ...
    {% endfor %}
{% endif %}

# 条件控制

{% if 条件1 %}
    ...
{% elif 条件2 %}
    ...
{% else %}
    ...
{% endif %}

# 循环控制

{% for i in data %}
    ...
{% else %}
    ...
{% endfor %}

完整示例

from flask import Flask, render_template

@app.route('/')
def index():
    return 'Hello,flask!'

@app.route('/<username>')
def name(username):
    return 'Hello,' + str(username)

@app.route('/hello/<username>')
def hello(username):
    return render_template('hello.html',
        username = username
    )

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