samedi 2 avril 2016

How to instantiate a checkbox in the form constructor in Django?

Here is my code:

models.py

class CheckModel(models.Model):
    a = models.BooleanField()
    b = models.BooleanField()

forms.py

class CheckForm(forms.Form):
    CHOICES = (('a', 'A',), ('b', 'B'))
    check_form= forms.MultipleChoiceField(required=False,
                                                   choices=CHOICES,
                                                   widget=forms.CheckboxSelectMultiple,
                                                   label='')

views.py

settings = CheckModel.objects.get(pk=1)
a = settings.aenter code here
b = settings.b

if request.method == 'GET':
    form = CheckForm(initial = {'a': a, 'b': b}) # <-- doesn't check or uncheck the boxes on GET

The last code is my problem. How do I instantiate a form with checkboxes checked or unchecked from variables that I get from the database?




Aucun commentaire:

Enregistrer un commentaire