Last updated on July 6, 2025 pm
安装
pip install flask
基础入门
- 实例化一个Flask对象,创建一个路由,实现一个基本的页面
1 2 3 4 5 6 7 8 9 10
| from flask import Flask app = Flask(__name__) def index(): return "Hello world"
if __name__ == "__main__": app.debug = True app.run()
|
route 之后的字符串表示拼接到 localhost:5000
后面的参数
<> 之中的传参可以带入到页面当中
1 2 3 4
| @app.route('/name/<name>',methods=['GET','POST'])
def putName(name): return f'Hello {name}'
|
HTTP方法
1
| from flask import Flask,request
|
1
| request.args.get('传参变量名')
|
eg:
1 2 3 4 5 6 7 8 9
| @app.route('/method',methods=['GET','POST'])
def method(): if request.method == 'GET': name = requset.args.get('name') return "GET !!!" + name elif request.method == 'POST': name = request.form['name'] return "POST !!!" + name
|
redirect 重定向
1
| from flask imprt Flask,request,redirect,url_for
|
eg:
1 2 3 4 5 6 7 8 9
| @app.route('/login',method=['GET']) def login(): key = "123456" UserKey = request.args.get('key') if UserKey == key: return redirect(url_for('login_ed')) @app.route('/login_ed',method=['GET'])
|
模板渲染
- 首先在项目目录下建一个
template
文件夹,建立 html文件
1 2 3 4 5 6 7 8 9 10 11
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h1>Hello {{name}}</h1> </body> </html>
|
- 渲染 index.html 的同时 向文件传参 将name变量渲染
1 2 3 4 5 6
| from flask import Flask,render_template
@app.route('/name/<name>') def name(name): return render_template('index.html',name=name)
|
模板继承
1 2 3 4 5 6 7 8 9 10 11 12
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{% block title %}BasePage{% endblock %}</title> </head> <body> {% block content %}main{% endblock %} homepage </body> </html>
|
1 2 3 4 5 6 7 8
| {% extends "base.html" %}
{% block title %}Home Page{% endblock %}
{% block content %} <h2>Welcome to the Home Page!</h2> <p>Hello {{name}}</p> {% endblock %}
|
控制结构
1 2 3 4 5
| {% if name == "Lixiney" %} <p>Hello admin</p> {% else %} <p>Hello {{name}}</p> {% endif %}
|
1 2 3
| {% for person in name %} <p>{{person}}</p><br> {% endfor %}
|
过滤器
过滤器用于在模板中格式化和处理变量数据
1 2
| <p>{{ name|capitalize }}</p> <p>{{ price|round(2) }}</p>
|
name|capitalize }}``` 将 name 变量的值首字母大写1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| ```{{ price|round(2) }}``` 将 price 变量的值四舍五入到小数点后两位
## 宏和模板包含
> 在一个模板中插入另一个模板的内容
* 创建宏
创建 templates/macos.html
```jinja2 {% macro render_item(item) %} <div> <h3>{{ item.title }}</h3> <p>{{ item.description }}</p> </div> {% endmacro %}
|
创建 templates/index.html
1 2 3 4 5 6
| {% from "macros.html" import render_item %}
<h1>Items</h1> {% for item in items %} {{ render_item(item) }} {% endfor %}
|
安全性
用户输入的内容会被自动转义,以避免恶意代码注入
模板上下文
1 2 3 4
| @app.route('/profile/<username>') def profile(username): user = {'name': username, 'age': 25} return render_template('profile.html', user=user)
|
1 2
| <h1>{{ user.name }}</h1> <p>Age: {{ user.age }}</p>
|
表单处理
Python开发-Flask框架
https://blog.lixey.top/posts/483d9cef/