2

I have a form which has a input radio like the following:

<form class="search" action="{{ url_for('np.bkg') }}" method="post">

    <input type="text" name="query" style="max-width:700px" placeholder="Search over bkg..." id="query" value="{{query}}" autocomplete="on" required>
    <button type="submit"><i class="fa fa-search"></i></button>
    <div>
    <input type="radio" name="searchType" id="kmatch" value="kmatch" > match </input>
    <input type="radio" name="searchType" id="kextraction" value="kextraction"> extract </input>
    </div>
    
</form>

There is no default value for the radio button. Then I have this line:

  search_type = request.form['searchType', None]

However, it reports this error when making a reqeust:

File "/bkg/myenv/lib/python3.7/site-packages/werkzeug/datastructures.py", line 443, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: ('searchType', None)

I thought the default None value can prevent this error, but it didn't.

How to fix it?

marlon
  • 6,029
  • 8
  • 42
  • 76
  • Does this answer your question? [flask handle form with radio buttons](https://stackoverflow.com/questions/31662681/flask-handle-form-with-radio-buttons) – dben Nov 21 '21 at 20:36

1 Answers1

0

For those who still have this problem it should be like this,

search_type = request.form.get('searchType', None)
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
MarArcz
  • 1
  • 1