返回变量以及渲染模板

时间:2017-06-09 12:28:31

标签: python flask

我写了一个def,它将渲染一个Flask模板。但是,我也想要这个相同的def来返回一个变量(dicts列表),用于我的下一个def。如何返回变量以及渲染模板?

所以我的div就这样结束了:

return render_template("portfolio.html", portfolioData = portfolioData[0], weights = weights, strategies = strategies, restrictions = restrictions, allowable_restrictions = allowable_restrictions)

我还希望它将权重作为变量返回,所以我可以在我的下一个开发中使用它,它开始如下:

@app.route("/rebalance", methods=["GET", "POST"])
def rebalance(weights):
port_id = session["port_id"]
if request.method == "GET":
    # load security info
    securities = db.execute("SELECT * FROM portfolios WHERE portfolio_id = :portfolio_id", portfolio_id = port_id)

知道怎么做吗?至少这就是我假设我可以重新使用一个def中的一个defid列表到另一个中.....

TXS! 巴特

1 个答案:

答案 0 :(得分:1)

I think you have some problem in the design of your application.

In Flask a view function returns only one thing - a rendered template or json or redirects to some other route etc. If you need to share some list during one request between various functions then you can use flask.g.

If you need to persist something between the requests then use flask session.