【问题标题】:None value returned when trying to access variables across flask sessions尝试跨烧瓶会话访问变量时没有返回值
【发布时间】:2021-09-20 01:13:44
【问题描述】:
from flask import Flask, session, request, render_template
import model   //model.py

app = Flask(_name__)
session.key = "randomkey"

@app.route("/", methods = ['POST', 'GET'])
def hello():
    if request.method == 'POST':
        //Some cide that takes input from index.html and stores in python variables
        prediction = model.make_prediction(modelinput)
        session["prediction"] = prediction
    return render_template("index.html")


@app.route("/prediction", methods = ['POST', 'GET'])
def submit():
    final_prediction = session.get("prediction", None)
    return render_template("prediction.html", predic = final_prediction)

现在,即使我使用会话变量在会话之间传递值,我也会得到一个 None 值作为输出。这是为什么呢?

【问题讨论】:

  • 我在我的系统上运行了你的代码,没有数据库,它工作正常。检查prediction = model.make_prediction(modelinput) 这一行是否给出了所需的输出。
  • 是的,该行给出了所需的输出。我通过使用 print 函数在终端中打印变量进行了验证。

标签: python flask session


【解决方案1】:

尝试在您的代码中添加这一行:

from flask import Flask, session, request, render_template
import model   //model.py

app = Flask(_name__)
app.config["SECRET_KEY"] = "topSecret"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    相关资源
    最近更新 更多