【问题标题】:Getting HTML content of a flask route from a different route从不同的路由获取烧瓶路由的 HTML 内容
【发布时间】:2015-03-27 19:54:58
【问题描述】:

假设我的代码是这样的:

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 这样的库只会显示“不允许访问”错误。

【问题讨论】:

    标签: python flask


    【解决方案1】:

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

    def test():
        hello = hello_world()
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多