【问题标题】:Using variables across Flask routes [duplicate]在 Flask 路由中使用变量 [重复]
【发布时间】:2018-04-05 04:07:01
【问题描述】:

我正在学习 Flask,并且有一个关于在路由上下文中使用变量的问题。例如,我的 app.py:

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

@app.route("/index")
def index():

   a=3
   b=4
   c=a+b

return render_template('index.html',c=c)

@app.route("/dif")
def dif():

   d=c+a

   return render_template('dif.html',d=d)


if __name__ == "__main__":
   app.run()

在路由/dif下,变量d是取已经计算出来的c和a的值来计算的。如何在页面之间共享变量c和a,从而计算出变量d并渲染到dif.html中?

谢谢

【问题讨论】:

    标签: python flask routes jinja2


    【解决方案1】:

    如果您不想使用Sessions 跨路由存储数据的一种方法是请参阅下面的更新):

    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)
    

    注意:访问/dif之前需要先访问/index


    更新

    根据大卫主义的评论,上面的代码不是生产友好的,因为它不是线程安全的。我已经用processes=10 测试了代码并在/dif 中得到了以下错误:

    错误显示data.adata.c 的值在processes=10 时保持None


    因此,它证明了我们不应该在 Web 应用程序中使用全局变量

    我们可以使用Sessions 或数据库来代替全局变量。

    在这个简单的场景中,我们可以使用会话来实现我们想要的结果。 使用会话更新代码:

    from flask import Flask, render_template, session
    app = Flask(__name__)
    # secret key is needed for session
    app.secret_key = 'dljsaklqk24e21cjn!Ew@@dsa5'
    @app.route("/index")
    def index():
        a=3
        b=4
        c=a+b
        session["a"]=a
        session["c"]=c
        return render_template("home.html",c=c)
    
    @app.route("/dif")
    def dif():
        d=session.get("a",None)+session.get("c",None)
        return render_template("second.html",d=d)
    
    if __name__ == "__main__":
        app.run(processes=10,debug=True)
    

    输出:

    【讨论】:

    • 如果我通过 pip install xyz 安装一个包。然后,在 route.py 中导入 xyz。然后,在路由功能中,如果我通过 xyz.varname.append 或 xyz.varname=100 分配或改变 xyz 中的变量。现在,这会影响所有其他用户线程中的 varname 吗?
    【解决方案2】:

    您可以通过在 URL 中编写从 HTML 传递的变量来使用烧瓶中的变量。 ,

    from flask import Flask, render_template
        app = Flask(__name__)
    
        @app.route("/index")
        def index():
    
           a=3
           b=4
           c=a+b
    
        return render_template('index.html',c=c)
    
        @app.route("<variable1>/dif/<variable2>")
        def dif(variable1,variable2):
    
           d=c+a
    
           return render_template('dif.html',d=d)
    
    
        if __name__ == "__main__":
    

    您的 html 将如下所示: 形式:

    <form action="/{{ variable1 }}/index/{{ variable2}}" accept-charset="utf-8" class="simform" method="POST"
    

    作为href:

    <a href="{{ url_for('index',variable1="variable1",variable2="variable2") }}"><span></span> link</a></li>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-30
      • 2019-10-08
      • 2022-10-13
      • 2016-05-13
      • 2017-04-01
      • 1970-01-01
      相关资源
      最近更新 更多