变量 session['article']
似乎不能在函数之间共享,用下面的代码,我不明白为什么。我们的目标是访问变量 session['article']
从函数 def game()
因此,为了能够在 game.html
通过变量 message
.这里是我的python Flask后台。
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)
这里是我的 index.html
是如下。
<!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>
而我 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>
当我点击 "Valider "按钮后,我得到了以下信息。
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'
我已经找到了问题的解决方法。return redirect(url_for('game'))
是错误的,我把return redirect('/game')
. 我把它拿掉了 d = session['article']
还 message = d
,从 def game()
. 最后,我一直 {{session.article}}
在我的'game.html'中。
就这样,谢谢你