title: Flask session variable cannot be shared between functions
body: <p>The variable <code>session['article']</code> seems not to be sharable between functions, with the following code, I don't understand why. The objective is to access the variable <code>session['article']</code> from the function <code>def game()</code>, so that to be able to render it in the <code>game.html</code> via the variable <code>message</code>.
  Here is my python Flask backend:</p>
  
  <pre><code>from flask import Flask, render_template, session, request, redirect, flash
  from getpage import getPage
  
  app = Flask(__name__)
  
  app.secret_key = b'TwIsTeR@9458'
  
  @app.route('/', methods=['GET','POST'])
  def index():
      return render_template('index.html', message="Bonjour, monde !")
  
  @app.route('/new_game', methods=['GET','POST'])
  def new_game():  
      if request.method == 'POST':
          session['article'] = request.form['name']
          return redirect(url_for('game'))
  
  @app.route('/game', methods=['GET','POST'])
  def game():    
      d = session['article']
      return render_template('game.html', message=d)
  
  # récupération du titre
  # 
  # Si vous définissez de nouvelles routes, faites-le ici
  
  if __name__ == '__main__':
      app.run(debug=True)
  </code></pre>
  
  <p>here is my <code>index.html</code> is as following:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="fr"&gt;
    &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;link rel="stylesheet" type="text/css" href="/static/style.css"&gt;
      &lt;title&gt;Tous les chemins mènent à la philosophie&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      {% block body %}
        {# Début du bloc "body", dont vous devez modifier le contenu #}
        &lt;p&gt;{{ message }}&lt;/p&gt;
        &lt;form action="/new_game" method="post"&gt;
          &lt;input type="text" id="name" name="name" required minlength="4" maxlength="8" size="10"&gt;
          &lt;input type="submit" name="submit_button" value="valider"&gt;
        &lt;/form&gt;
        {# Fin du bloc "body" #}
      {% endblock %}
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and my <code>game.html</code>:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="fr"&gt;
    &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;link rel="stylesheet" type="text/css" href="/static/style.css"&gt;
      &lt;title&gt;Game rendering&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
          {% extends "index.html" %}
            {% block body %}
            {{ message }}
          {% endblock %}
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Once I hit the button "Valider", I got the message following:</p>
  
  <pre><code>KeyError
  KeyError: 'article'
  
  Traceback (most recent call last)
  File "/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
  return self.wsgi_app(environ, start_response)
  File "/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
  response = self.handle_exception(e)
  File "/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
  reraise(exc_type, exc_value, tb)
  File "/opt/anaconda3/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
  raise value
  File "/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
  response = self.full_dispatch_request()
  File "/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
  rv = self.handle_user_exception(e)
  File "/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
  reraise(exc_type, exc_value, tb)
  File "/opt/anaconda3/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
  raise value
  File "/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
  rv = self.dispatch_request()
  File "/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
  return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/twister/Desktop/MesCours/INF344HTTPWEB/TP2 à rendre/philosophie/philosophie.py", line 26, in game
  d = session['article']
  File "/opt/anaconda3/lib/python3.7/site-packages/werkzeug/local.py", line 378, in &lt;lambda&gt;
  __getitem__ = lambda x, i: x._get_current_object()[i]
  File "/opt/anaconda3/lib/python3.7/site-packages/flask/sessions.py", line 84, in __getitem__
  return super(SecureCookieSession, self).__getitem__(key)
  KeyError: 'article'
  </code></pre>