Python中使用多个路由/页面的全局变量

时间:2016-04-11 05:28:12

标签: python flask routes global-variables

您好,我想知道是否可以在路线之间使用全局变量(我使用烧瓶)

基本上我有一个页面/路由加载多项选择题并将正确答案存储在变量答案中,我想要另一个路径来使用该答案变量。

我尝试创建一个全局变量,然后将其分配给第一个路径中的答案值,但我无法使其工作。

以下是相关代码 -

global glob_answer
glob_answer = "Answer"    # initialized to this value for testing (I get nothing)

@app.route('/quiz')
def quiz():
    cursor = g.conn.execute("select actor_name, categ, a_name, year, title from win_actor order by random() limit 1")

    c_names  = []
    category = ""
    alias    = ""
    year     = ""
    title    = ""
    for name in cursor:
        c_names.append(name[0]) # can also be accessed using result[0]

#SETTING GLOBAL VARIABLE EQUAL TO ANSWER
        answer  = name[0]
        glob_answer = answer
        category = name[1]
        year     = str(name[3])
        title    = name[4]
        cursor2 = g.conn.execute("select alias from awards where a_name = " + "'" + name[2] + "'" + " limit 1")
        for columns in cursor2:
            alias = columns[0]
        cursor2.close()
    cursor.close()
    cursor = g.conn.execute("select * from actor limit 4")
    for name in cursor:
        if name[0] not in c_names:
            c_names.append(name[0]) # can also be accessed using result[0]
    cursor.close()
    shuffle(c_names)
    context = dict(data = c_names, categ = category, award_name = alias, year = year, title = title, answer = answer)
    return render_template("quiz.html", **context)

#Second Route
@app.route('/answer')
def answer():
#TRYING TO PASS VALUE TO NEW LOCAL VARIABLE
    global glob_answer 
    answer = glob_answer   
    return render_template("answer.html")

当我显示“答案”时我的HTML中的变量,没有任何显示

0 个答案:

没有答案