如何在其他函数烧瓶中使用局部变量?

时间:2015-05-13 02:13:37

标签: python variables flask

每次网页访问者点击'(主页)时,我都会调用一个功能。我也会在其他函数中使用该函数的结果。有关如何做到这一点的任何建议?

@app.route('/')
def home():
   store = index_generator() #This is the local variable I would like to use 
   return render_template('home.html')

 @app.route('/home_city',methods = ['POST'])
 def home_city():
   CITY=request.form['city']
   request_yelp(DEFAULT_LOCATION=CITY,data_store=store) """I would like to use the results here"""
   return render_template('bank.html')

1 个答案:

答案 0 :(得分:4)

请参阅Flask session docs。你需要做一些小的设置。

from flask import session

@app.route('/')
def home():
   store = index_generator()
   session['store'] = store
   return render_template('home.html')

 @app.route('/home_city',methods = ['POST'])
 def home_city():
   CITY=request.form['city']
   store = session.get('store')
   request_yelp(DEFAULT_LOCATION=CITY,data_store=store)
   return render_template('bank.html')