daveoncode
daveoncode

Reputation: 19618

Django forms: checkboxes are displayed without value

I'm unable to save my form (a ModelForm) properly, since django displays checkboxes without a value (I would expect value="true" on every fields, both checked than unchecked... but that's not the case). When I submit the form, no data is received in the POST!

The following is a piece o my template:

 <div>
      {{form.displayAge.label_tag}}
      {{form.displayAge}} 
      {{form.displayAge.errors}}
 </div>

{{form.displayAge}} is rendered in this way:

<input checked="checked" type="checkbox" name="displayAge" id="id_displayAge">

BUT... since it has no value, checking/unchecking the checkbox is helpless! What should I do? I would like to avoid typing form fields by hand!

Upvotes: 2

Views: 2266

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599956

No, there is no need for a value field. If the checkbox is checked, the browser will submit "on" as the value by default if none is supplied.

If you're not getting this value in your view, something else is wrong. Note that since you're using Django forms, you shouldn't be checking request.POST manually anyway: use form.cleaned_data.

Upvotes: 1

Related Questions