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>
相关推荐
TF男孩1 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
该用户已不存在6 小时前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
站大爷IP8 小时前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python
用户83562907805113 小时前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
c8i13 小时前
python中类的基本结构、特殊属性于MRO理解
python
liwulin050614 小时前
【ESP32-CAM】HELLO WORLD
python
Doris_202314 小时前
Python条件判断语句 if、elif 、else
前端·后端·python
Doris_202314 小时前
Python 模式匹配match case
前端·后端·python
这里有鱼汤15 小时前
Python量化实盘踩坑指南:分钟K线没处理好,小心直接亏钱!
后端·python·程序员
大模型真好玩15 小时前
深入浅出LangGraph AI Agent智能体开发教程(五)—LangGraph 数据分析助手智能体项目实战
人工智能·python·mcp