0

I was working on a Flask program and wanted to use a variable (present inside a function) outside the function.

So, I researched and stumbled upon sessinos storage. I used this to store my variable and use it outside the function.

But I'm getting this error

RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed an active HTTP request. Consult the documentation on testing for information about how to avoid this problem.

This is the code for app.py :

from flask import Flask, render_template, request, session

app = Flask(__name__)
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'

@app.route('/', methods =["GET", "POST"])
def gfg():
    if request.method == "POST":
       first_name = request.form.get("fname")
       session['first_name'] = first_name
       return "Your name is " + first_name
    return render_template("index.html")

with app.app_context():
    first_name = session.get('first_name')
    print(first_name)
if __name__ == '__main__':
    app.run(debug=True)


1 Answer 1

1

I think what you're trying to do isnt the right approach. Simply put, you cannot use session.anything outside of the @app.route() function, because session requires an active request to be in process.

however, if you want to use the variable first_name at a time when there's no active request for some reason, you can use a python global:

from flask import Flask, render_template, request, session
app = Flask(__name__)
app.secret_key = 'change_this! and dont include it in questions!'
first_name = "nothing yet"  # this is the initial value when the app runs

@app.route('/', methods =["GET", "POST"])
def gfg():
    if request.method == "POST":
       global first_name  # this will use the global scope for this variable
       first_name = request.form.get("fname")  # this will update the global variable now!

       return "Your new name is " + first_name
    return "Your current name is " + first_name


if __name__ == '__main__':
    app.run(debug=True)
Sign up to request clarification or add additional context in comments.

7 Comments

Hey, Thanks. I tried this but it's not working. It is still returning the old value of first_name i.e 'Nothing Yet' : (
yes, thats because the line "with app.app_context()" is only run once, when the app boots up. if you make a second route for example def fgf(): print(last_name), it will then print the last_name you set when calling gfg(). the line with app.app_context is only ever used when the app runs, you need to use the variable later, after you called gfg() at least once.
I made this a little bit more clear by adding another route with the url /print . if you get or post on that route, it will print the last known value of first_name
I added a print(fgf()) in the code but it returns None
That too, is happening before I enter an input in the webpage
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.