lundi 28 septembre 2015

Toggle Checkbox in Django and Write to Database

I have some model

from django.db import models

class Model1(models.Model):
     title = models.CharField(max_length=300)
     boolean1 = models.BooleanField(default=False)
    boolean2 = models.BooleanField(default=False)

and am currently displaying all database entries in a template via the following bootstrap table:

<div class = 'table-responsive'>
<table class='table table-striped'>
    <thead>
        <th>Title</th>
        <th>Boolean 1</th>
        <th>Boolean 2</th>
    </thead>
    {% if model1_list %}
    {% for Model1 in model1_list %}
    <tbody>
        <tr>
        <td>{{ Model1.title }}</td>
        <td>{{ Model1.boolean1 }}</td>
        <td>{{ Model1.boolean2 }}</td>
        </tr>
    {% endif %}
    {% endfor %}
    </tbody>
 </table>
 </div>

I'd like to display the boolean values as checkboxes rather than text, and to be able to check and uncheck the values and have it be reflected in the database (i.e., if the database is checked, the value should be True).

What would be a way to implement this?




Aucun commentaire:

Enregistrer un commentaire