Python flask路由間傳遞變量實(shí)例詳解
我查了一下解決這個問題的辦法,一般是設(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>
運(yùn)行結(jié)果
在路由index上的結(jié)果
在路由dif上的結(jié)果
代碼見https://github.com/qingnvsue/flask中的difrouters文件夾
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Java封裝數(shù)組實(shí)現(xiàn)包含、搜索和刪除元素操作詳解2. 淺談SpringMVC jsp前臺獲取參數(shù)的方式 EL表達(dá)式3. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)4. Gitlab CI-CD自動化部署SpringBoot項目的方法步驟5. python基于socket模擬實(shí)現(xiàn)ssh遠(yuǎn)程執(zhí)行命令6. Django-migrate報錯問題解決方案7. JAVA上加密算法的實(shí)現(xiàn)用例8. 使用Python和百度語音識別生成視頻字幕的實(shí)現(xiàn)9. idea打開多個窗口的操作方法10. ASP中解決“對象關(guān)閉時,不允許操作。”的詭異問題……
