I'm making a flask webpage which dynamically generates an HTML table from a database, and a checkbox for each record beside it. I want the user to select multiple records and delete them all at once, but I can't seem to figure out how to know which records are selected. The code for the html table:
<thead>
<tr>
<th scope="col" class="col-md-1">Delete</th>
<th scope="col" class="col-md-5">Device Name</th>
<th scope="col" class="col-md-6">Device MAC</th>
</tr>
{% for row in data %}
<tr>
<td>
<input type="checkbox" name="checkbox{{row .index}}"
value={{row .index}}{% if row .like== 1 %} checked {% else %} {% endif %}>
{{row .like}}
</td>
<td>
{{row[0]}}
</td>
<td>
{{row[1]}}
</td>
</tr>
{% endfor %}
</thead>
</table>
I was thinking about using the requests.form way of getting the 'checked' value, but it's not on a form, so how should I access this? And how do I do this for every record in the table, so I can delete them from the database?
Edit:
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
if request.form['submit_button'] == 'Add':
devAdd = [request.form['addDN'], request.form['addMAC']]
error = addDevice(devAdd)
return render_template('index.html',
errorAdd=error,data=loadDB())
else:
#Delete
else:
return render_template('index.html', data=loadDB())
The if request.form['submit_button'] == 'Add': part is another button that adds a record to the table, so the code for the delete process needs to be in the else: #Delete
part.
However, the table is not part of the form, so should I create its own form? And will it cause conflict?