从不同的路由获取 HTML 个烧瓶路由的内容

Getting HTML content of a flask route from a different route

假设我的代码是这样的:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/test')
def test():
    # Get HTML contents of route "/"
    return "test"

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

现在在 test 函数中,我想以编程方式获取路由 /(即 Hello World!)的 HTML 内容。有没有办法做到这一点 ?请注意,我不想使用像 request 这样的库来执行此操作,因为在我的原始用例中,两个路由函数都经过身份验证并且使用像 request 这样的库只会显示 "Access not allowed"错误。

只是一个函数,你可以调用它。

def test():
    hello = hello_world()

但是,如果您有要在多个处理程序中显示的内容,您可能应该将其提取到一个单独的函数中,您可以从两个路由调用该函数。