I tried to implement bootstrap switch for getting two values ON or OFF. Everything works fine when the switch is ON but when OFF the attribute cannot be read from the route itself.
https://getbootstrap.com/docs/5.0/forms/checks-radios/#switches Below is My code in Bootstrap:
<form action="/test2" method="POST">
<div class="form-check form-switch">
<input class="form-check-input" data-toggle="switch" type="checkbox" id="flexSwitchCheckDefault" value="false" name="test1">
<label class="form-check-label" for="flexSwitchCheckDefault">Test 1</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" name="test2">
<label class="form-check-label" for="flexSwitchCheckChecked">Test 2</label>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
For getting Values in Backend
@app.post('/test2')
def test2():
formdata = flask.request.form
print(formdata)
print(formdata['test1'])
return "success", 200
When switch is OFF the it gives an error
raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'test1'
but when On everything works fine. Much Appriciated!