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

Flask路由复用函数结果:如何返回两函数结果至render_template?

解决Flask路由同时返回两个函数结果的问题

其实核心问题就是你没把两个函数的结果都作为参数传递给render_template,我给你捋清楚步骤,直接上代码示例就明白啦:

首先,在你的路由函数里,先分别获取两个函数的结果,再一起传给模板:

@app.route('/your-target-route')
def your_main_route():
    # 第一步:先调用第一个函数,把结果存到变量里
    related_interfaces_data = get_related_interfaces()
    # 第二步:把第一个结果作为输入传给第二个函数,拿到第二个结果
    net_info_data = get_net_info(related_interfaces_data)
    # 第三步:把两个结果都作为参数传给render_template,模板就能同时拿到啦
    return render_template('your-page.html', 
                           related_interfaces=related_interfaces_data,
                           net_info=net_info_data)

然后在你的HTML模板里,就可以分别调用这两个变量来展示内容了,比如:

<!-- 展示第一个函数的结果 -->
<div class="interface-section">
  <h3>相关接口列表</h3>
  <p>{{ related_interfaces }}</p>
</div>

<!-- 展示第二个函数的结果 -->
<div class="net-info-section">
  <h3>网络详情信息</h3>
  <p>{{ net_info }}</p>
</div>

你之前只能拿到第二个函数的结果,大概率是在return的时候只传了第二个函数的结果,没把第一个也带上。只要把两个结果都作为关键字参数传给render_template,模板里就能同时访问到两个函数的返回值啦。

内容的提问来源于stack exchange,提问作者Red Coleman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:21:57