-1

I want to retrieve in my python app the selected value in a html/javascript form.

Here is my html code :

{% block content %}
<form method="post">
   <div class="form-group">
       <label for="title">Numéro</label>
       <input type="number" name="NUMERO"
              placeholder="666, etc" class="form-control" required
              value="{{ request.form['NUMERO'] }}"/>
   </div>
<div class="form-group">
       <label for="TYPE_VOIE">Type voie</label>
       <select class="form-control" id="TYPE_VOIE" required >
          <option>Rue</option>
          <option>Impasse</option>
          <option>Place</option>
          <option>Boulevard</option>
          <option>Avenue</option>
      </select>  value="{{ request.form['TYPE_VOIE'] }}"
</div>

Here is python code:

    if request.method == 'POST':
        numero=int(request.form['NUMERO'])
        type_voie= request.form['TYPE_VOIE'] 

and now the error message:

werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'TYPE_VOIE' ...

In fact, I don't know where to put value="{{ request.form['TYPE_VOIE'] }}"

Can you help me ? Thanks in advance

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
bong101
  • 3
  • 4

1 Answers1

0

You should add a name attribute to your combo box like you did for the text box, thus:

<select class="form-control" id="TYPE_VOIE" name="TYPE_VOIE" required >

Basically, Flask does not "see" a form field named 'TYPE_VOIE' in your POST request.

Kate
  • 1,809
  • 1
  • 8
  • 7
  • Thanks you very much....In my form, do I delete all attributes value="..." ? – bong101 Oct 08 '20 at 08:45
  • On the contrary you should have value tags for all your form fields, if you want to retrieve their values in the POST request. – Kate Oct 08 '20 at 19:12