2

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!

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm confused... is this a Django feature? Since html specs don't confirm what you wrote: w3.org/TR/html-markup/… ...and a simple test in the browser confirm instead that nothing is submitted in a similar scenario (simple html form with "get" method and a checkbox with no value)
arrrgh! The problem is this: I was configuring my forms with Django's User model not my UserProfile model... just replaced MyForm(request.POST, instance=request.user) with MyForm(request.POST, instance=request.user.userprofile) and all is ok! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.