Flask 是一款轻量级的 Python Web 框架,简单易用,适合初学者快速搭建网站和 API。
本篇文章将带你从环境搭建开始,手写一个基础的 Flask 应用,实现路由、模板渲染和表单处理。


一、安装 Flask


bash

复制编辑

pip install Flask


二、创建第一个 Flask 应用


python

复制编辑

from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return "Hello, Flask!" if __name__ == '__main__': app.run(debug=True)

运行后打开浏览器访问 http://127.0.0.1:5000/,你会看到页面显示 “Hello, Flask!”。


三、路由与视图函数

Flask 通过装饰器 @app.route 定义 URL 路由,视图函数返回响应内容。


python

复制编辑

@app.route('/hello/<name>') def hello_name(name): return f"Hello, {name}!"

访问 http://127.0.0.1:5000/hello/花花,会显示 “Hello, 花花!”。


四、模板渲染

Flask 内置 Jinja2 模板引擎,用于渲染动态 HTML。

  • 创建目录 templates/,新建 index.html


html

复制编辑

<!DOCTYPE html> <html> <head><title>欢迎</title></head> <body> <h1>Hello, {{ name }}!</h1> </body> </html>

  • 修改视图函数:


python

复制编辑

from flask import render_template @app.route('/welcome/<name>') def welcome(name): return render_template('index.html', name=name)

访问 /welcome/花花,页面会显示 “Hello, 花花!”。


五、表单处理示例

  • HTML 表单 templates/form.html


html

复制编辑

<form action="/submit" method="post"> 名字: <input type="text" name="username" /> <input type="submit" value="提交" /> </form>

  • 路由处理提交:


python

复制编辑

from flask import request @app.route('/submit', methods=['GET', 'POST']) def submit(): if request.method == 'POST': username = request.form.get('username') return f"提交成功,您好 {username}!" return render_template('form.html')


六、运行 Flask 应用


bash

复制编辑

python app.py

开启调试模式后,代码修改会自动重载,方便开发。


七、总结

Flask 组件 功能
app.route 定义路由
render_template 渲染 HTML 模板
request 获取请求数据
debug 模式 方便调试自动重载

Flask 简洁灵活,适合快速构建 Web 服务和 API,后续可以结合数据库、用户认证等功能进行扩展。

https://bigu.wang

https://www.bigu.wang

https://binm.wang

https://www.binm.wang

https://bint.wang

https://www.bint.wang

https://biop.wang

https://www.biop.wang

https://bits.wang

https://www.bits.wang

https://bjqb.wang

https://www.bjqb.wang

https://bjsm.wang

https://www.bjsm.wang

https://bleo.wang

https://www.bleo.wang

https://ono.wang

https://www.ono.wang

https://onz.wang

https://www.onz.wang

https://opo.wang

https://www.opo.wang

https://osm.wang

https://www.osm.wang

https://osn.wang

https://www.osn.wang

https://ovi.wang

https://www.ovi.wang

https://oxq.wang

https://www.oxq.wang

https://oti.wang

https://www.oti.wang

https://owu.wang

https://www.owu.wang

https://piq.wang

https://www.piq.wang

https://qmi.wang

https://www.qmi.wang

https://qki.wang

https://www.qki.wang

https://ref.wang

https://www.ref.wang

https://sak.wang

https://www.sak.wang

https://sar.wang

https://www.sar.wang

https://sfa.wang

https://www.sfa.wang

https://sfe.wang

https://www.sfe.wang

https://sgo.wang

https://www.sgo.wang

https://sku.wang

https://www.sku.wang

https://ycxjz.cn

https://www.ycxjz.cn

https://bnbmhomes.cn

https://www.bnbmhomes.cn

https://jinjianzuche.com

https://www.jinjianzuche.com

https://ahswt.cn

https://www.ahswt.cn

https://szwandaj.cn

https://www.szwandaj.cn

https://psbest.cn

https://www.psbest.cn

https://shanghai-arnold.cn

https://www.shanghai-arnold.cn

https://zgsscw.com

https://www.zgsscw.com

https://shxqth.cn

https://www.shxqth.cn

https://wdxj.cn

https://www.wdxj.cn

https://jad168.com

https://www.jad168.com

https://ultratrailms.cn

https://www.ultratrailms.cn

https://tztsjd.cn

https://www.tztsjd.cn

https://csqcbx.cn

https://www.csqcbx.cn

https://qazit.cn

https://www.qazit.cn

https://ahzjyl.cn

https://www.ahzjyl.cn

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐