mercredi 7 décembre 2016

How to find value of HTML radio button in using Python and flask?

So I've created the following form with radio buttons, that is submitted via "POST":

<form style="margin: auto">
            <label>
              <input type="radio" name="activity" value="0"/>sedentary
            </label>
            <label>
              <input type="radio" name="activity" value="1"/>lightly active (1-3 days a week)
            </label>
            <label>
              <input type="radio" name="activity" value="2"/>moderately active (3-5 days a week)
            </label>
            <br>
             <label>
              <input type="radio" name="activity" value="3"/>heavy exercise (6-7 days a week)
            </label>
            <label>
              <input type="radio" name="activity" value="4"/>very heavy exercise (exercising twice a day or working physical job)
            </label>
          </form>

My problem is that I don't know how to access the results of this in my Python code. I'm able to access values submitted via text forms on the same page, so I don't think my POST is the issue (I'm using Flask, so request.form.get("field") does the trick there). Research shows that people have previously suggested "request.form['activity']", but this results in a 404 error; people have also suggested "request.form.get('activity','1'), but this just returns a value of '1' whether or not the radio button is even pressed.

Any help would be appreciated!




Aucun commentaire:

Enregistrer un commentaire