-1

I have a pretty basic question here, I'm sure, but it's giving me a big headache. I have a flask/python program that, essentially, after a form is submitted, a value of a variable changes and the page refreshes, now including text that shows that variable.

Here is some example code to iterate my idea:

@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)
...

Any help? I'm a novice here, so I'm probably just doing something dumb.

Rachel K.
  • 1
  • 1

2 Answers2

1

Variable assignment should use = instead of ==.

@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'
khan
  • 7,005
  • 15
  • 48
  • 70
0

You should define your variable at the beginning of your routes file. For example :)

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'
vi_ral
  • 369
  • 4
  • 19