0

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):

  1. Send a query to DB to update that same row where I will set field “setting_enab” to “True”
  2. Update “setting_descr” for the same row with some comment (string)

Can you please guide me on how I can achieve this?

2
  • Use django's ModelForm and formsets, it'll make your life easier. Commented Dec 6, 2016 at 12:08
  • Do you mean by this that I can't acheive what I want with my intended solution? Also I had a look to your suggestion, it seem a bit hard for a beginer but I will look at it again. Commented Dec 8, 2016 at 9:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.