You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask蓝图设置URL_PREFIX后POST请求路由错误致404的解决求助

问题:Flask蓝图设置URL前缀后POST请求路由错误

我在app.register_blueprint(views, url_prefix='/my-app')中设置了url_prefix='/my-app',但点击按钮发送POST请求时,请求会路由到/process而非预期的/my-app/process,导致404 Not Found错误。只有将url_prefix改为'/'时请求才能正常工作,请问如何让请求正确路由到/my-app/process?

相关代码示例

main.py

from website import create_app

app = create_app()

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

init.py(设置URL_prefix的文件)

from flask import Flask

def create_app():
    app= Flask(__name__)
    app.config['SECRET_KEY'] = 'GFTTR687YVXTR5GF96R4EDF'

    from .views import views

    app.register_blueprint(views, url_prefix='/my-app')

    return app

views.py

from flask import Blueprint, render_template, request,jsonify,redirect,url_for,session,Flask
from json import dumps
import pandas as pd

views = Blueprint('create_flow_chart', __name__)


@views.route('/')
def home():
    # do something
    return render_template("create-flow-chart.html",category=category, steps =dumps(text),product=product)


@views.route('/process', methods=['POST','GET'])
def send_selection():
    # do something
    return redirect(url_for('.do_create',steps=steps))


@views.route('/create')
def do_create():
    # do something
    return render_template("create-flow-chart.html",category=category, steps = dumps(steps),product=product)

create-flow-chart.html(按钮所在页面)

<h2>TOOL</h2> 
<div class="row mb-3">
    <label>Product</label>
    <form method="POST" action="/process">
        <div class="custom-select">
            <select id="product" name="product", method="GET" action="/">
                {% for products in product %}
                    <option value = "{{products}}">{{products}}</option>
                {% endfor %}
            </select>
        </div>

        <label>Categories/NC</label>
        <div class="custom-select">
            <select id="category" name="category">
                {% for categories in category %}
                    <option value = "{{categories}}">{{categories}}</option>
                {% endfor %}
            </select>
        </div>
        <div style="  position: absolute;
        bottom: 25px;
        right: 0px;">
            <button type="submit">OK</button>
        </div>
    </form>
</div>

解决方案

方法1:使用url_for生成正确路由(推荐)

问题根源在于HTML表单的action属性写死了绝对路径/process,没有包含蓝图的URL前缀。Flask的url_for函数可以自动根据蓝图和视图函数生成带前缀的完整路径,无需手动拼接。

修改create-flow-chart.html中的form标签:

<form method="POST" action="{{ url_for('create_flow_chart.send_selection') }}">
  • create_flow_chart是蓝图的名称(在views = Blueprint('create_flow_chart', __name__)中定义)
  • send_selection是对应的视图函数名
  • url_for会自动生成/my-app/process这个正确路径,无论后续蓝图前缀或路由如何修改,都能保持正确匹配。

方法2:改用相对路径(可选)

如果不想使用url_for,可以将action改为相对路径process(去掉开头的斜杠):

<form method="POST" action="process">

这种方式会基于当前页面的URL(/my-app/)自动拼接成/my-app/process,但缺点是当页面URL结构变化时需要手动调整,灵活性不如方法1。

验证效果

修改后,点击提交按钮时,POST请求会正确发送到/my-app/process,匹配蓝图中定义的路由,不会再出现404错误。

内容的提问来源于stack exchange,提问作者yyii mm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 04:56:34