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

Mit*_*tch 8 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')
Run Code Online (Sandbox Code Playgroud)

Pep*_*zza 5

请参阅Flask 会话文档。你需要做一些小的设置。

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')
Run Code Online (Sandbox Code Playgroud)