title: Flask : RuntimeError: Working outside of request context
body: <p>I was working on a Flask program and wanted to use a variable (present inside a function) outside the function.</p>
  <p>So, I researched and stumbled upon sessinos storage. I used this to store my variable and use it outside the function.</p>
  <p>But I'm getting this error</p>
  <blockquote>
  <p>RuntimeError: Working outside of request context.</p>
  <p>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.</p>
  </blockquote>
  <p>This is the code for app.py :</p>
  <pre><code>from flask import Flask, render_template, request, session
  
  app = Flask(__name__)
  app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
  
  @app.route('/', methods =[&quot;GET&quot;, &quot;POST&quot;])
  def gfg():
      if request.method == &quot;POST&quot;:
         first_name = request.form.get(&quot;fname&quot;)
         session['first_name'] = first_name
         return &quot;Your name is &quot; + first_name
      return render_template(&quot;index.html&quot;)
  
  with app.app_context():
      first_name = session.get('first_name')
      print(first_name)
  if __name__ == '__main__':
      app.run(debug=True)
  
  
  </code></pre>