3

As stated in the title. BooleanField wont return True even if checked. This one is driving me nuts!

First I retrieve checkboxes based on a list of field id's based on a model.

forms.py

class MyForm(forms.Form): def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) fields = Fields.objects.all() for field in fields: field_name = field.ID self.fields[field_name] = forms.BooleanField(required=False, widget=forms.CheckboxInput(attrs={'class':'checkbox-small'}), label=field_name) 

The handler never returns True even if checked on in the DOM.

models.py

def myFormHandler(request): siteDictionary = getDictionary(request) if request.method == 'POST': form = MyForm(request.POST, error_class=DivErrorList, auto_id='%s') if form.is_valid(): fields = Fields.objects.all() for field in fields: if form.cleaned_data[field.ID]: print "Finally returned true!" else: form = MyForm() siteDictionary['form'] = form return render_to_response('page.html', siteDictionary, context_instance=RequestContext(request)) 

Any ideas? Thanks for your help.

EDIT Here is the template

<table cellspacing="0" cellpadding="0" border="0" class="table-all"> <thead> <tr> <th><input type="checkbox" name="check" class="checkall checkbox-small" /></th> <th>Name</th> </tr> </thead> <tbody> {% for field in form %} <tr> <td>{{ field }}</td> <td>{{ field.label }}</td> </tr> {% endfor %} </tbody> </table> <div id="pager" class="pager"> <form method="post" action=".">{% csrf_token %} <button type="submit" class="green"><span>Submit</span></button> </form> </div> 
1
  • 1
    What does the template look like? Commented Nov 27, 2011 at 20:22

2 Answers 2

2

Your fields aren't inside your HTML form element, so aren't submitted.

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

1 Comment

Yessir! This was part of the problem anyways (and I had everything inside of the form before but I took it out for some reason out of frustration I think). Alasdair's suggestion was also necessary. My field ID's were integers and converting them to strings fixed my problem as well.
0

Is field.ID an integer? If so, you need to coerce it to a string when generating field_name.

class MyForm(forms.Form): def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) fields = Fields.objects.all() for field in fields: field_name = str(field.ID) self.fields[field_name] = forms.BooleanField(required=False, widget=forms.CheckboxInput(attrs={'class':'checkbox-small'}), label=field_name) 

3 Comments

Yes! This was apart of my problem as well. (my form placement was wrong too). Question: Why does it matter if it's a string vs integer? I was still following an integer indexing throughout the request. Thanks again for your help! I'd upvote but I don't have 15 pts yet =(
Try printing request.POST in your view. You'll see that all the keys are strings, so Django cannot match the post data to your fields with integers for names.
As an aside, I would avoid using names and ids that start with digits (setting auto_id='%s' means the field id is a number) This isn't valid in HTML 4 (see the spec), although it's ok in HTML5. It might not matter in some browsers, but sooner or later it might cause some odd behaviour that takes an afternoon to track down.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.