A
A
Anton2021-01-24 11:14:30
Flask
Anton, 2021-01-24 11:14:30

How do I pass a list of values ​​through Flask's checkbox?

I have a table that is generated from a database. I need to get all the values ​​of checked fields in a checkbox. But I can only send one value. Here is the code that generates the table from the database:

<div>
         <form name="markers" method="post">
                     <button type="submit" name="marker">Маркировки</button>
          </form>
</div>
              {% for item in awaiting_deliver %}
              <tr>
               <form name="markers" method="post">
                  <td>
                     <input class="form-check-input" type="checkbox" name="check_box" value="{{ item.posting_number }}">
                  </td>
                </form>
                 <td class="text-muted">{{ item.shipment_date }}</td>

                 {% endfor %}

So no value is accepted. If I do as follows, then only one value is accepted
{% for item in awaiting_deliver %}
              <tr>
               <form name="markers" method="post">
                  <td>
                     <input class="form-check-input" type="checkbox" name="check_box" value="{{ item.posting_number }}">
                     <button type="submit" name="marker">Маркировки</button>
                  </td>
                </form>
                 <td class="text-muted">{{ item.shipment_date }}</td>

                 {% endfor %}

Here is the processing of the request:
if "marker" in request.form:
            markers = request.form.getlist("check_box")
            print(markers)

It is necessary to make sure that the submit button is separate from the table (as in the first option) and sends all values ​​where the checkbox is checked.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-01-24
@NeiroNx

I forgot brackets for multiple values:
... name="check_box[]" ...
and all inputs must be inside a single form tag.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question