Python flask路由间传递变量实例详解

作者:yaominghui 时间:2021-03-13 16:30:16 

我查了一下解决这个问题的办法,一般是设定全局变量,今天介绍一种新办法

上代码difrouters.py


from flask import Flask, render_template
app = Flask(__name__)

class DataStore():
 a = None
 c = None

data = 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上的结果

Python flask路由间传递变量实例详解

在路由dif上的结果

Python flask路由间传递变量实例详解

代码见https://github.com/qingnvsue/flask中的difrouters文件夹

来源:https://www.cnblogs.com/qingnvsue/p/13019888.html

标签:Python,flask,路由,变量
0
投稿

猜你喜欢

  • python实现xml转json文件的示例代码

    2023-09-29 23:52:50
  • 为你总结一些php信息函数

    2023-10-28 09:46:59
  • Python的iOS自动化打包实例代码

    2022-04-18 01:15:19
  • 详解Python中的条件判断语句

    2022-05-03 09:33:28
  • python判断字符串是否是json格式方法分享

    2023-01-02 20:50:37
  • 5 个简单实用的 CSS 属性

    2010-03-10 11:00:00
  • python开发一个解析protobuf文件的简单编译器

    2021-08-11 12:01:22
  • 利用python、tensorflow、opencv、pyqt5实现人脸实时签到系统

    2022-03-12 16:00:44
  • 使用Python实现控制摄像头的方法详解

    2023-01-15 14:38:12
  • python实现音乐下载的统计

    2021-07-07 20:08:54
  • python之pyqt5通过按钮改变Label的背景颜色方法

    2021-04-03 22:59:52
  • 如何在Win下mysql备份恢复命令

    2010-03-03 17:23:00
  • python Copula 实现绘制散点模型

    2023-07-24 14:02:37
  • 使用PDB简单调试Python程序简明指南

    2022-07-27 21:03:28
  • php中支持多种编码的中文字符串截取函数!

    2023-09-27 02:08:15
  • mysql 主从服务器的简单配置

    2009-09-06 12:06:00
  • Python RuntimeError: thread.__init__() not called解决方法

    2022-12-22 17:11:46
  • python 正则表达式 概述及常用字符

    2023-03-03 07:16:15
  • python利用datetime模块计算时间差

    2021-10-07 01:02:04
  • python中ndarray数组的索引和切片的使用

    2022-10-12 13:49:55
  • asp之家 网络编程 m.aspxhome.com