Django-4.2.8, django-crispy-forms-2.1, checkboxes not return a list

30 views
Skip to first unread message

b...@tanners.org

unread,
Dec 8, 2023, 8:59:29 PM12/8/23
to Django users
I have a Django ModelForm (below) that I include a customized html snipped.
And I am using django-crispy forms.

Added the clean() method to help explain my problem.

```python
class ProfileForm(forms.ModelForm):
    self.helper.layout = Layout(
    Fieldset(
        "Triage {{ settings.triage }}",
        HTML('{% include "snippets/problem_choice.html" %}'),
    ),
)

def clean(self):
    unclean = self.data
    super().clean()
    data = self.cleaned_data
```

The simplified `problem_choice.html` snippet is a table of radio buttons and checkboxes.
```html
<tr>
<td>Problem A</td>
<td><input type="radio" name="choice_1" value="1"></td>
<td><input type="radio" name="choice_2" value="1"></td>
<td><input type="radio" name="choice_3" value="1"></td>
<td><input type="checkbox" name="willing_to_judge" value="1"></td>
<td><input type="checkbox" name="not_willing_to_judge" value="1">1</td>
<td><input type="checkbox" name="not_willing_to_judge" value="x">x</td>
<td><input type="checkbox" name="not_willing_to_judge" value="y">y</td>
<td><input type="checkbox" name="not_willing_to_judge" value="z">z</td>
</tr>
```
I put a breakpoint at the `unclean = self.data()` in the `clean()` method. 
I expect if I click (check) the not_willing_to_judge checkboxes 1, x, y, 
and z I'd see a list in unclean data, [1, x, y, z] but I only see `not_willing_to_judge: 'z'`

If I click (check) 1, x, y, and leave z unchecked, I get `not_willing_to_judge: 'y'`

Does anyone see the problem?

Can someone confirm that I should be getting a list of the checked items in the unclean data?

Thanks.


Vincenzo Celano

unread,
Dec 9, 2023, 6:03:57 AM12/9/23
to django...@googlegroups.com
Ciao
I think the problem is in the NAME of the checkbox. 
4 checkbox with same name, the form will consider only the last one.
I think, if you check for example 1 and x you will get: 'not_willing_to_judge: 'x''. Try to rename checkbox: not_willing_to_judge_1, not_willing_to_judge_2,  not_willing_to_judge_3,  not_willing_to_judge_4.

By

Reply all
Reply to author
Forward
0 new messages