Python Flask 快速入门:搭建你的第一个 Web 应用

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值