3

So, I need to grab an input value from an html form and use it in flask. Here's an example of python and html for reference:

python:

@app.route("/post_field", methods=["GET", "POST"])

def need_input():

    for key in request.form["post_field"]:    

        if key == "value1":
            #do the thing I want#

html:

<form action="/post_field" method="post">
    <input type="hidden" name="this_name" value="value1" />
    <input type="submit" value="Press Me!"/>
</form> 

I get a 400 error when I click that Press Me input.

2
  • What port is your development server running on? Commented Sep 17, 2017 at 6:27
  • @Kyle, localhost:5000 Commented Sep 17, 2017 at 7:12

1 Answer 1

3

This seems to be working for me. I can access the form data via request.form that functions like a dictionary. You can iterate over the form key, values with request.form.items() (assuming it's python3).

app.py

from flask import Flask, request

app = Flask(__name__)

@app.route("/post_field", methods=["GET", "POST"])
def need_input():
    for key, value in request.form.items():
        print("key: {0}, value: {1}".format(key, value))

@app.route("/form", methods=["GET"])
def get_form():
    return render_template('index.html')

templates/index.html

<html>
<head>
</head>
<body>
  <form action="/post_field" method="post">
    <input type="hidden" name="this_name" value="value1" />
    <input type="submit" value="Press Me!"/>
    </form>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.