Bart Koolhaas
Bart Koolhaas

Reputation: 351

Return variable as well as render template

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

Upvotes: 2

Views: 1175

Answers (1)

Nurjan
Nurjan

Reputation: 6073

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.

Upvotes: 1

Related Questions