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

0

2 Answers 2

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'
Sign up to request clarification or add additional context in comments.

Comments

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'

2 Comments

This still gives me the error: UnboundLocalError: local variable 'counter' referenced before assignment
Please remember to include counter = 'one' before the method call

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.