简体   繁体   中英

How are jinja values returned in this context?

I am new to learning jinja/flask and have a relatively low comprehension thus far. If someone could help with this, that would be great. I am having some trouble understanding how to access the values of the variable here. I made a feeble attempt with no success. I get errors when I click my "submit" button to try to get my name to print on the next page after "Welcome, ".

Alright, I have an index page with a form and I would like to return the submitted value on the POST page as shown below. However, I am having trouble with the syntax. This is built in Pycharm with Flask using python/jinja2/html. That is where some of my confusion exists I believe.

EDIT_1: I understand this topic has several previous postings, but I cannot seem to apply what those posts explain. Actually, the very code I have in here is from another post I found on here.

Here is the Python File:

from flask import Flask, render_template, request

app = Flask(__name__)


@app.route("/")
def index():
    return render_template("index.html")


@app.route("/username", methods=['POST', 'GET'])
def username():
    if request.method == 'POST':
       user = request.form
        return render_template("username.html", username=user)
    return print("Something went wrong...smash your computer!")


if __name__ == '__main__':
    app.run(debug=True)

Here is the Index file that contains the form in which I would like to obtain the username:

...
<div id="wrapper">
    <div id="title_head">
    <p class="title"> Gauntlet  </p>
    </div>
    <div id="working_area">
    <form action = "http://localhost:5000/username" method = "POST">
        <p>Enter Username: <input type = "text" name = "Name" /></p>
        <p><input type = "submit" value = "SUBMIT" /></p>
    </form>
    </div>
</div>
</body>
</html>

And finally, here is the username POST page (I will include the relevant part here):

...
<body>
<div id="wrapper">
   <div id="title_head">
        <p class="title"> Gauntlet  </p>
    </div>
    <div id="working_area">
        {% for user in username %}
        <p>Welcome, {{ username.user }}</p>
        {% endfor %}

    </div>
</div>
</body>

The problem lies with the code(flask) you use to parse the form data with. You need to supply the key of the form element in which you are entering the data.

Add user=request.form['Name'] to the code and you will have the required value in the variable username.Then you can do something like :

return render_template("username.html", username=user)

First, change user=request.form to username=request.form["Name"] .

And then, change return render_template("username.html", username=user) to render_template("username.html", username=username) .

Last, modify the template file of username.html :

...
<body>
    <div id="wrapper">
        <div id="title_head">
            <p class="title"> Gauntlet  </p>
        </div>
        <div id="working_area">
            <p>Welcome, {{ username }}</p>
        </div>
    </div>
</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM