Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

python - Post values from an HTML form and access them in a Flask view

I have an HTML form that gets posted to a Flask route. However, request.form is empty. If I try to access one of the values by id, I get a 400 error. How do I post values from an HTML form and access them in Flask?

<form method="POST">
  <input id="my_input" type="text" value="{{ email }}">
  <input id="my_submit" type="submit" value="Submit">
</form>
@app.route('/page', methods=['POST', 'GET'])
def get_page():
    if request.method == 'POST':
        print(request.form)  # prints ImmutableMultiDict([])
        print(request.form['my_input'])  # raises 400 error
    return render_template('page.html')
question from:https://stackoverflow.com/questions/65905523/post-method-with-bootstrap-flask-and-html

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your input doesn't have a name attribute. That is what the client will pass along to the server. Flask will raise a 400 error if you access a form key that wasn't submitted.

<input name="my_input" id="my_input" type="text" value="{{ email }}">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...