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)