在尝试使用会话的视图之间传递变量时出现问题,无法正常工作。说我有两页,a home
和page2
。我有一个flask应用程序,它将从接收用户输入home
并在上打印输出page2
。
例如,如果启动我的应用程序,则将其视为主页:
在单击提交之后,接下来要发生的事情page2
是生成显示刚刚输入的内容:
home
都应显示在突出显示的部分中。I have the following app.py file:
from flask import Flask, render_template, request, session
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def home():
stringval = ''
if request.method == 'POST' and 'stringval' in request.form:
stringval = request.form.get('stringval')
session["stringvalue_topass"] = stringval
return render_template('page2.html', stringval = stringval)
return render_template("home.html")
@app.route('/page2', methods=['GET', 'POST'])
def page2():
stringvalue_get = session.get('stringvalue_topass')
return render_template('page2.html', stringvalue_get = stringvalue_get)
if __name__ == '__main__':
app.run(debug=True)
The following home.html:
<!doctype html>
<h1>Enter Value </h1>
<div class="main">
<form class="pure-form" method="POST" action="/page2">
stringval:<br>
<input type="text" name="stringval"><br>
<button type="submit" class="pure-button pure-button-primary" value="Submit">Submit!</button>
</form>
</div>
</body>
And the following page2.html
<!doctype html>
<h1>You have selected </h1>
<div class="main">
{% if stringvalue_get %}
<pre>
{% print(stringvalue_get) %}
</pre>
{% endif %}
</div>
</body>
Okay, there are a few issues here. Firstly, the action
attribute of your form in home.html
is set to "/page2"
. This means that when the form is submitted, the POST request is going to the /page2
endpoint rather than to the /home
endpoint, where you have written the code for handling the form submission. We can fix this by just deleting the action
attribute, as this means the form will post to then endpoint that loaded it - in this case /home
.
Secondly, Flask sessions cannot be used without setting a secret key to encrypt the session. This can be done by assigning a value to app.secret_key
, like so:
app = Flask(__name__)
app.secret_key = b"my_secret_key"
最后,您可以直接从模板访问对象,而不必像这样将字符串传递到模板:(render_template('page2.html', stringval = stringval)
,也要注意stringval_get = stringval
)session
。因此,总而言之,我们可以将您的应用程序代码更改为:
app.py:
from flask import Flask, render_template, request, session
app = Flask(__name__)
app.secret_key = b"my_secret_key"
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST' and 'stringval' in request.form:
session["stringvalue_topass"] = request.form.get('stringval')
return render_template('page2.html')
return render_template("home.html")
@app.route('/page2', methods=['GET', 'POST'])
def page2():
return render_template('page2.html')
您的模板可以:
home.html:
<!doctype html>
<h1>Enter Value </h1>
<div class="main">
<form class="pure-form" method="POST">
stringval:<br>
<input type="text" name="stringval"><br>
<button type="submit" class="pure-button pure-button-primary" value="Submit">Submit!</button>
</form>
</div>
</body>
page2.html:
<!doctype html>
<h1>You have selected </h1>
<div class="main">
{% if 'stringvalue_topass' in session %}
<pre>
{% print(session["stringvalue_topass"]) %}
</pre>
{% endif %}
</div>
</body>