-1

The variable session['article'] seems not to be sharable between functions, with the following code, I don't understand why. The objective is to access the variable session['article'] from the function def game(), so that to be able to render it in the game.html via the variable message. Here is my python Flask backend:

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)

here is my index.html is as following:

<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="/static/style.css">
    <title>Tous les chemins mènent à la philosophie</title>
  </head>
  <body>
    {% block body %}
      {# Début du bloc "body", dont vous devez modifier le contenu #}
      <p>{{ message }}</p>
      <form action="/new_game" method="post">
        <input type="text" id="name" name="name" required minlength="4" maxlength="8" size="10">
        <input type="submit" name="submit_button" value="valider">
      </form>
      {# Fin du bloc "body" #}
    {% endblock %}
  </body>
</html>

and my game.html:

<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="/static/style.css">
    <title>Game rendering</title>
  </head>
  <body>
        {% extends "index.html" %}
          {% block body %}
          {{ message }}
        {% endblock %}
  </body>
</html>

Once I hit the button "Valider", I got the message following:

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 à 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 <lambda>
__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'
davidism
  • 121,510
  • 29
  • 395
  • 339
twister9458
  • 29
  • 1
  • 1
  • 7

1 Answers1

0

I've found the solution of my problem: return redirect(url_for('game')) was wrong, instead I put return redirect('/game'). And I removed d = session['article'], and also message = d, from def game(). And finally, I kept {{session.article}} in my 'game.html`.

That was it. Thank you

twister9458
  • 29
  • 1
  • 1
  • 7