返回变量以及渲染模板 [英] Return variable as well as render template
问题描述
所以我的div结束这样:
return render_template(portfolio.html,portfolioData = portfolioData [0],权重=权重,策略=策略,限制=限制,allowable_restrictions = allowable_restrictions)
我也希望它将权重作为变量返回,所以我可以在我的下一个开发中使用它,如下所示: p>
@ app.route(/ rebalance,methods = [GET,POST])
def rebalance (权重):
port_id = session [port_id]
if request.method ==GET:
#载入安全信息$ b $证券= db.execute(SELECT *从投资组合WHERE portfolio_id =:portfolio_id,portfolio_id = port_id)
任何想法如何做到这一点?至少这是我如何假设我可以重新使用一个清单中的一个清单到另一个.....
发送!
Bart
我认为你的应用程序的设计有一些问题。
在Flask中,一个视图函数只返回一个东西 - 渲染的模板或者json或者重定向到其他的路由等等。如果你需要在一个请求之间共享某个列表函数,那么你可以使用 flask.g
。
如果您需要在请求之间保留某些内容,请使用flask会话。
I have written a def which will render a Flask template. However, I also want this same def to return a variable (list of dicts) to be used in my next def. How can I return both a variable as well as render a template?
So my div ends like this:
return render_template("portfolio.html", portfolioData = portfolioData[0], weights = weights, strategies = strategies, restrictions = restrictions, allowable_restrictions = allowable_restrictions)
and I also want it to return weights as a variable, so I can use it in my next dev, which starts as follows:
@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)
Any idea how to do this? At least this is how I assume I can re-use a list of dicts made in one def into another.....
Txs! Bart
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.
这篇关于返回变量以及渲染模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!