Python / Flask重新定义和初始化变量

时间:2018-11-27 03:02:52

标签: python variables flask

我敢肯定,我在这里有一个非常基本的问题,但这让我头疼不已。我有一个flask / python程序,从本质上讲,提交表单后,变量的值会更改,页面会刷新,现在包括显示该变量的文本。

下面是一些示例代码来迭代我的想法:

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        if counter == 'one':
            #stuff happens here
            counter == 'two'
        else:
            if counter == 'two':
                #stuff happens
                counter == 'three'
    else:
        counter == 'one'
return render_template("index.html",counter=counter)
...

有帮助吗?我是这里的新手,所以我可能只是在做一些愚蠢的事情。

2 个答案:

答案 0 :(得分:1)

变量分配应使用=而不是==

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        if counter == 'one':
            #stuff happens here
            counter = 'two'
        else:
            if counter == 'two':
                #stuff happens
                counter = 'three'
    else:
        counter = 'one'

答案 1 :(得分:0)

您应该在路由文件的开头定义变量。例如:)

from flask import render_template

counter = 'one'
@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        if counter == 'one':
            #stuff happens here
            counter = 'two'
        else:
            if counter == 'two':
                #stuff happens
                counter = 'three'
    else:
        counter = 'one'