Python flask路由間傳遞變量實例詳解
我查了一下解決這個問題的辦法,一般是設(shè)定全局變量,今天介紹一種新辦法
上代碼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> 結(jié)果c={{ c }}</body></html>
dif.html
<html><head> <title>different router</title></head><body> 結(jié)果d={{ d }}</body></html>
運行結(jié)果
在路由index上的結(jié)果

在路由dif上的結(jié)果

代碼見https://github.com/qingnvsue/flask中的difrouters文件夾
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python 制作查詢商品歷史價格的小工具2. Python 合并拼接字符串的方法3. Python sublime安裝及配置過程詳解4. Python插件機(jī)制實現(xiàn)詳解5. Python3 json模塊之編碼解碼方法講解6. ASP基礎(chǔ)知識VBScript基本元素講解7. Linux刪除系統(tǒng)自帶版本Python過程詳解8. ASP.NET MVC使用jQuery ui的progressbar實現(xiàn)進(jìn)度條9. python 使用事件對象asyncio.Event來同步協(xié)程的操作10. Python 利用Entrez庫篩選下載PubMed文獻(xiàn)摘要的示例

網(wǎng)公網(wǎng)安備