MultiselectCheckBox Post not passing data in request

Hi,

I have rendered a model.form multi select checkbox, but when POSTing the data, the checkbox value is not being passed in the request. The other fields are past and added to the database.

def add_project_view(request):
    form = ProjectForm
    next = request.POST.get('next', '/')
    if request.method == 'POST':
        form = ProjectForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/')
        
    else:
        form = ProjectForm()
    return render(request, 'add_project.html', {'form':form})
<div class="parsley-checkbox wd-250 mg-b-0" id="cbWrapper2">												
    {% for checkbox in form.platform %}
        <label class="ckbox"><input data-parsley-class-handler="#cbWrapper2" data-parsley-errors-container="#cbErrorContainer2" data-parsley-mincheck="2" name="{{ checkbox.name }}" type="checkbox" value="{{ checkbox.id }}"><span>{{checkbox}}</span></label> 
    {% endfor %}											
</div>

Payload:

csrfmiddlewaretoken: Kf7y5IyS31oSjOJkR9HqztlE3eorPrv8b1yzEJ5QVfiFkE4WpsDLaIr1Uu3mvamS
name: GroupName2
website: https://test.com
twitter_handle: tom
platforms = [
    ('windows', 'windows'),
    ('linux', 'linux'),
    ('iOS', 'iOS'),
]
class ProjectForm(ModelForm):
    class Meta:
        model = Project
        fields = ('name','website','platform','twitter_handle')

        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control wd-xl-250'}),
            'website': forms.URLInput(attrs={'class': 'form-control wd-xl-250'}),
            'platform': forms.CheckboxSelectMultiple(attrs={'class':'ckbox'}),
            'twitter_handle': forms.TextInput(attrs={'class': 'form-control wd-xl-250'}),
      
        }

So it doesn’t seem to pass in the platform field?

Thanks

I don’t think we’re seeing enough of the template. Seeing the model would help, too.

Sorry Ken, forgot to paste my model.

class Project(models.Model):
    platforms = [
    ('windows', 'windows'),
    ('linux', 'linux'),
    ('iOS', 'iOS'),
]
    name = models.CharField(max_length=50, blank=False, unique=True)
    website = models.URLField(max_length=50, blank=False)
    platform = models.CharField(max_length=200, blank=True,choices=platforms)
    twitter_handle = models.CharField(max_length=50, blank=False, null=True)


    def __str__(self):
        return str(self.name)

Below is the form

<form action="#" class="parsley-style-1" id="selectForm2" name="selectForm2" method="POST">
    {% csrf_token %}
    {{ form.non_field_errors }}
    <div class="">
        <div class="d-md-flex mg-b-20">
            <div class="parsley-input" id="fnWrapper">
                <label>Project Name: <span class="tx-danger">*</span></label>
                {{ form.name }}
            </div>
            <div class="parsley-input mg-md-l-20 mg-t-20 mg-md-t-0" id="lnWrapper">
                <label>Project Website: <span class="tx-danger">*</span></label>
                {{ form.website }}
            </div>
            <div class="parsley-input mg-md-l-20 mg-t-20 mg-md-t-0" id="lnWrapper">
                <label>Project Twitter Handle: <span class="tx-danger">*</span></label>
                {{ form.twitter_handle }}
            </div>
        </div>
    </div>											
        <div class="parsley-checkbox wd-250 mg-b-0" id="cbWrapper2">												
            {% for checkbox in form.platform %}
                <label class="ckbox"><input data-parsley-class-handler="#cbWrapper2" data-parsley-errors-container="#cbErrorContainer2" data-parsley-mincheck="2" name="{{ checkbox.name }}" type="checkbox" value="{{ checkbox.id }}"><span>{{checkbox}}</span></label> 
            {% endfor %}											
        </div>										
    </div>
</form>

Your platform field in the model is a CharField. It doesn’t make sense to render that in the form as a multi-select checkbox - only one platform can be selected in that case. I’m guessing there’s some interaction between the two causing this to not work.

That would make sense, but how do I record the selection of the checkbox if it’s not a CharField? How else does a multi-selection checkbox work?

A multi-selection checkbox would be used with (perhaps) something like a ForeignKey related table, or (with some work) a JSONField or a PostgreSQL ArrayField.

Otherwise, if you’re just looking to allow for the selection of one option from a list of choices, then you can use a CheckBox widget for that field. (Or, perhaps more appropriately, radio buttons.)