Forums

Clear variables after "GET" request

Hi! I'd like to clear my variables after each "GET" for my web app.

My code declares two empty sets, builds those sets based on "POST" data from a form, and displays the web app with those variables if they exist. Like below!

entry = []
artist_info = []

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "GET":
        return render_template("main_page.html", entry=entry, artist_info=artist_info)
    entry.clear()
    artist_info.clear()
    # entry and artist_info are defined based on the "POST" data

Currently, the variables clear upon every "POST" request; I'd like them to clear after every "GET" request. I can't figure out how to do this as a successful GET seems to require a return, preventing further code.

Any advice is appreciated, thanks so much!

You could render template first, then clear the entry and return the rendered template in the "GET" branch of your code. Using global state in a web app that uses more than one worker (which is the case in all paid account) can be tricky though. See this help page for more details.

Thanks so much! Very helpful answer to my actual question (embarrassed I didn't think of that) and to problems I didn't even bring up.

Glad we could help!