温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Flask, Passing User Entered Data Between Views

python - Flask,在视图之间传递用户输入的数据

发布于 2020-03-27 16:04:03

尝试使用会话的视图之间传递变量时出现问题,无法正常工作。说我有两页,a homepage2我有一个flask应用程序,它将从接收用户输入home并在上打印输出page2


例如,如果启动我的应用程序,则将其视为主页

主页

  • 这部分工作正常,我可以输入一个值。

在单击提交之后,接下来要发生的事情page2是生成显示刚刚输入的内容

第2页

  • 输入的任何字符串值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>

查看更多

查看更多

提问者
jpf5046
被浏览
35
CDJB 2020-01-31 16:52

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 = stringvalsession因此,总而言之,我们可以将您的应用程序代码更改为:

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>

发布
问题

分享
好友

手机
浏览

扫码手机浏览