Warm tip: This article is reproduced from stackoverflow.com, please click
flask forms html python session

Flask, Passing User Entered Data Between Views

发布于 2020-03-27 15:44:07

Problem transferring variables across views I tried using sessions and could not get the connection to work. Say I have two pages, a home and page2. I have a flask app that will take user input from the home and print out input on page2.


For example, if you start my app, you will see this as the home page:

Home Page

  • This part works fine, I am able to enter a value.

What I want to happen next, is after you click submit, page2 is generated showing what was just entered:

page2

  • Whatever string value was entered on home should show up in the highlighted portion.

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>

Questioner
jpf5046
Viewed
50
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"

Finally, instead of passing the string to the template like so: render_template('page2.html', stringval = stringval), (note also that this should be stringval_get = stringval), you can access the session object directly from templates already. So, in all, we can change your application code to:

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')

And your templates to:

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>