Flask目录结构路由重定向简单实例讲解——轻量级的 Python Web 框架

假设一个flask目录结构如下:

复制代码
my_flask_app/
│
├── app.py
├── routes/
│   ├── __init__.py
│   ├── ZhejiangProvince/
│   │   ├── __init__.py
│   │   ├── la.py
│   │   └── el.py
│   ├── GuangdongProvince/
│   │   ├── __init__.py
│   │   ├── la.py
│   │   └── el.py
│   ├── wia.py
├── static/
│   ├── css/
│   ├── js/
│   ├── img/
│   │   ├── LA.png
│   │   ├── EL.png
│   └── ...
├── templates/
│   ├── index.html
│   ├── layout.html
│   ├── ZhejiangProvince/
│   │   ├── la.html
│   │   ├── el.html
│   ├── GuangdongProvince/
│   │   ├── la.html
│   │   ├── el.html
│   └── ...
├── instance/
│   ├── config.py
├── config.py
├── requirements.txt
├── venv/
└── README.md

我们关心的是在index.html中点击图标或文字怎么跳转到另一个html页面。

这就需要两个东西:蓝图实例和蓝图名称。

**Step1:创建蓝图实例、**定义蓝图中的路由

python 复制代码
from flask import Blueprint

# 创建蓝图实例
zj_la_bp = Blueprint('zj_la', __name__)

@zj_la_bp.route('/LA', methods=['GET', 'POST'])
def LA():
    if request.method == 'POST':
        # 处理表单提交的代码
        pass
    return render_template('ZhejiangProvince/la.html')

'zj_la_bp' 是蓝图实例的变量名,用于在 Python 代码中引用和操作蓝图。
'zj_la' 是蓝图的名称字符串,用于标识蓝图并在生成 URL 时使用。

关于 **'zj_la_bp'和'zj_la'**的使用,可以简单记为: 在前端使用'zj_la',在后端使用zj_la_bp'。
(这里在前端和在后端是指在关于前端的代码中和在关于后端的代码中)。

在前端使用'zj_la',在后端使用zj_la_bp'。:

html 复制代码
例如:
  在后端:
     @zj_la_bp.route('/LA', methods=['GET', 'POST'])
  在前端:
     return redirect(url_for('zj_la.LAdocxGenerate'))
     <a href="{{ url_for('zj_la.LA') }}">

Step2:在 app.py 中注册蓝图:

python 复制代码
from flask import Flask, redirect, url_for, render_template
from routes.ZhejiangProvince.la import zj_la_bp

app = Flask(__name__)
app.secret_key = 'your_secret_key'

# 注册蓝图并指定 URL 前缀
app.register_blueprint(zj_la_bp, url_prefix='/zj/la')

@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html')

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

上述代码就会有一个URL: http://yourdomain.com/zj/la

Step3:在 index.html 模板中使用 url_for 函数生成 URL

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Index Page</title>
</head>
<body>
    <div class="index">
        <div class="LA">
            <!-- ************************************************************** -->
            <a href="{{ url_for('zj_la.LA') }}">
                <img src="{{ url_for('static', filename='img/LA.png') }}" alt="LA">
                <p>浙江省xxxx</p>
            </a>
            <!-- ************************************************************** -->
        </div>
    </div>
</body>
</html>
相关推荐
databook15 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室15 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三17 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户25191624271120 小时前
Python之语言特点
python
刘立军21 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机1 天前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i1 天前
django中的FBV 和 CBV
python·django
c8i1 天前
python中的闭包和装饰器
python
这里有鱼汤1 天前
小白必看:QMT里的miniQMT入门教程
后端·python