有一个蓝图定义了很多有用的路由,但我无法控制它(不能以任何方式更改它的代码)
尝试在不同的应用程序中重用它,但蓝图的端点之一必须重载。我怎样才能做到这一点?

我尝试在现有路线的基础上添加一条新路线到蓝图:

@blueprint.route('/my/route', methods=['PUT', 'POST'])
def my_new_view_func(program, project):
    # some new behavior for the endpoint

结果在 app.url_map.iter_rules() 中有重复的 url_rule :
<Rule '/my/route' (PUT, POST) -> my_view_func>,
<Rule '/my/route' (PUT, POST) -> my_new_view_func>,

当请求 /my/route 旧查看器时 my_view_func 被执行

我可以以某种方式摆脱旧的 url 规则吗?或者也许有更好的方法来覆盖路线?

最佳答案

我找到了 2 个解决方案。 首先 :

from flask import Flask, Blueprint


simple_page = Blueprint('simple_page', __name__, )


@simple_page.route('/my/route/')
def my():
    # for example it's a registered route somewhere...
    return 'default'


@simple_page.route('/my/route/')
def new_my():
    # new endpoint / should works instead my()
    return 'new'

# map of views which we won't register in Flask app
# you can store this somewhere in settings
SKIP_VIEWS = (
    # route, view function
    ('/my/route/', my, ),
)


class CustomFlask(Flask):

    def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
        # Flask registers views when an application starts
        # do not add view from SKIP_VIEWS
        for rule_, view_func_ in SKIP_VIEWS:  # type: str, func
            if rule_ == rule and view_func == view_func_:
                return
        return super(CustomFlask, self).add_url_rule(rule, endpoint, view_func, **options)


app = CustomFlask(__name__)
app.register_blueprint(simple_page)
app.run(debug=True)

第二种方式 :

two.py - 带有端点的默认蓝图
from flask import Blueprint

bp_two = Blueprint('simple_page2', __name__, )


@bp_two.route('/my/route/')
def default():
    return 'default'

test.py - 你的蓝图 + 应用
from flask import Flask, Blueprint

from two import bp_two

your_bp = Blueprint('simple_page', __name__, )


@your_bp.route('/my/route/')
def new_route():
    return 'new'


app = Flask(__name__)
# register blueprint and turn off '/my/route/' endpoint
app.register_blueprint(bp_two, **{'url_defaults': {'/my/route/': None}})
app.register_blueprint(your_bp)

app.run(debug=True)

运行应用程序。打开 /my/route/ 。您将看到默认端点未添加/工作。

希望这可以帮助。

关于python - 覆盖 flask 蓝图中的路线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49785019/

10-14 15:08