436 字
2 分钟

如何使用Python Flask模块

2025-09-07
0 阅读 / 0 访问

1. Flask简介#

Flask是一个轻量级的Python Web框架,它不包含ORM、表单验证等工具,而是让开发者自行选择。这使得Flask非常灵活,适合快速开发小型应用和API。

2. 安装Flask#

使用pip安装Flask非常简单:

Terminal window
pip install Flask

3. 第一个Flask应用#

创建一个名为 app.py 的文件,并添加以下代码:

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

运行应用:

Terminal window
python app.py

在浏览器中访问 http://127.0.0.1:5000/,你将看到 “Hello, World!”。

4. 路由和视图函数#

4.1. 动态路由#

你可以在URL中使用变量:

@app.route('/user/<username>')
def show_user_profile(username):
return f'User {username}'
@app.route('/post/<int:post_id>')
def show_post(post_id):
return f'Post {post_id}'

4.2. HTTP方法#

指定路由支持的HTTP方法:

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
return 'Logged in (POST)'
else:
return 'Show login form (GET)'

注意: 在实际使用中,request 对象需要从 flask 模块导入。

5. 模板渲染#

Flask使用Jinja2作为默认模板引擎。

5.1. 创建模板文件#

在项目根目录下创建一个名为 templates 的文件夹,并在其中创建 index.html 文件:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
</head>
<body>
<h1>{{ message }}</h1>
</body>
</html>

5.2. 渲染模板#

修改 app.py 文件:

from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html', title='主页', message='欢迎来到我的Flask应用!')
if __name__ == '__main__':
app.run(debug=True)

注意: render_template 函数需要从 flask 模块导入。

6. 静态文件#

在项目根目录下创建一个名为 static 的文件夹,用于存放CSS、JavaScript和图片等静态文件。

例如,创建一个 static/style.css 文件:

h1 {
color: blue;
}

修改 templates/index.html 引入静态文件:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1>{{ message }}</h1>
</body>
</html>

注意: url_for 函数需要从 flask 模块导入。

如何使用Python Flask模块
https://freebird2913.tech/posts/how-to-use-flask/
作者
free bird
发布于
2025-09-07
许可协议
CC BY-NC-SA 4.0