Python flask路由間傳遞變量實例詳解
我查了一下解決這個問題的辦法,一般是設定全局變量,今天介紹一種新辦法
上代碼difrouters.py
from flask import Flask, render_templateapp = Flask(__name__)class DataStore(): a = None c = Nonedata = DataStore()@app.route('/index')def index(): a=3 b=4 c=a+b data.a=a data.c=c return render_template('index.html',c=c)@app.route('/dif')def dif(): d=data.c+data.a return render_template('dif.html',d=d)if __name__ == '__main__': app.run(debug=True)
index.html
<html><head> <title>Home</title></head><body> 結果c={{ c }}</body></html>
dif.html
<html><head> <title>different router</title></head><body> 結果d={{ d }}</body></html>
運行結果
在路由index上的結果
在路由dif上的結果
代碼見https://github.com/qingnvsue/flask中的difrouters文件夾
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: