Python flask路由間傳遞變量實(shí)例詳解
我查了一下解決這個(gè)問題的辦法,一般是設(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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. IntelliJ IDEA安裝插件的方法步驟2. Docker 部署 Prometheus的安裝詳細(xì)教程3. idea重置默認(rèn)配置的方法步驟4. idea導(dǎo)入maven項(xiàng)目的方法5. IntelliJ IDEA設(shè)置自動(dòng)提示功能快捷鍵的方法6. idea設(shè)置代碼格式化的方法步驟7. IntelliJ IDEA調(diào)整字體大小的方法8. 通過Django Admin+HttpRunner1.5.6實(shí)現(xiàn)簡易接口測(cè)試平臺(tái)9. idea打開多個(gè)窗口的操作方法10. idea給項(xiàng)目打war包的方法步驟

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