san
san

Reputation: 11

How to create checkbox for single item in django

I know to create a multiple checkbox in the form if I have list of items.

forms.py

class GatewayForm(forms.Form):

GATEWAY_CHOICES = (

        ('Instamojo', 'Instamojo'),
        ('CCAvenue', 'CCAvenue'),
        ('ePaisa', 'ePaisa'))
gateway_name = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=GATEWAY_CHOICES)

template.html

I use a for loop to display the gateway_names.


Now how can I achieve the same when there is only one item in the list?

GATEWAY_CHOICES = (

        ('Instamojo', 'Instamojo'),

What form field or widget should I use?

Upvotes: 0

Views: 1328

Answers (2)

TeslaLDead
TeslaLDead

Reputation: 93

You can use a BooleanField type of field and set its required attribute to False

instamojo=forms.BooleanField(label='Instamojo',required=False)

This will create a check box like field that can either be checked or unchecked.

Upvotes: 0

Rohan Baddi
Rohan Baddi

Reputation: 479

I think you should add "required = False,"

gateway_name = forms.MultipleChoiceField(
        required = False,
        widget=forms.CheckboxSelectMultiple,
        choices=GATEWAY_CHOICES
)

I hope this might help you.

Upvotes: 1

Related Questions