Skip to content
Advertisement

Get the value of a checkbox in Flask

I want to get the value of a checkbox in Flask. I’ve read a similar post and tried to use the output of request.form.getlist('match') and since it’s a list I use [0], but it seems I’m doing something wrong. Is this the correct way to get the output or is there a better way?

JavaScript
JavaScript

Advertisement

Answer

You don’t need to use getlist, just get if there’s only one input with the given name, although it shouldn’t matter. What you’ve shown does work. Here’s a simple runnable example:

JavaScript

Submitting the form with both boxes checked prints ['world', 'davidism'] in the terminal. Note that the html form’s method is post so that the data will be in request.form.


While there are some cases where knowing the actual value or list of values of an field is useful, it looks like all you care about is whether the box was checked. In this case, it’s more common to give the checkbox a unique name and just check if it has any value at all.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement