I am trying to get the value of some checkboxes and update database for the rows that are checked. I don’t really know how to do it since I am only starting with Django/python.
Here is my model
class App_Setting(models.Model):
setting_name = models.CharField('Setting Name', max_length=30,
unique=True, blank=False,
default='None')
setting_enab = models.BooleanField('Enabled?', default=False)
setting_descr = models.CharField('Description', max_length=150,
blank=False, default='None')
Because I want the user to only be able to modify the “setting_enab” via checkbox I have built my template like this
{% extends "mainapp/base.html" %}
{% block title %}- Available Settings{% endblock %}
{% block mainframe %}
<h3>Available Settings</h3>
<div class="col-sm-8 col-lg-9">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Setting Name</th>
<th>Setting Enabled?</th>
<th>Setting Description</th>
</tr>
</thead>
<tbody>
{% for setting in object_list %}
<tr>
<td>{{ setting.setting_name }}</td>
<td><input type="checkbox" name="mycheckbox" class="checkthis" /></td>
<td>{{ setting.setting_descr }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock mainframe %}
I have read the following thread but can’t really make sense of it.
Django check if checkbox is selected
All I want to do is for each row that have the checkbox enabled (checked):
- Send a query to DB to update that same row where I will set field “setting_enab” to “True”
- Update “setting_descr” for the same row with some comment (string)
Can you please guide me on how I can achieve this?
ModelFormandformsets, it'll make your life easier.