Reputation: 279
I have a real annoying issue with form.is_valid()
always returning false on one of my forms, and this only happens when I manually define fields in a modelform, and in particular use ModelChoiceField and ModelMultipleChoiceFields passing in my own custom querysets
location = forms.ModelChoiceField(required=False, widget = forms.Select(), queryset = Location.objects.none())
I cannot find much in the way of good documentation about ModelForm.is_valid()
, and essentially, if I don't call it everything works fine. I am happy with this except for the fact that every Django forms example makes this call to is_valid()
.
So I guess my question is:
Thanks,
Mike
Upvotes: 0
Views: 7914
Reputation: 31
You should be checking your if your form is valid and then you can go from there but what I found worked when I was having the same problem was:
def add(request):
form = SearchForm()
add_form = AddForm()
if request.method == 'GET':
add_form = AddForm(request.GET)
if add_form.is_valid():
return render_to_response('inventory_app/add.html', {'form': form, 'add_form': add_form, 'message': 'Sent Sucessfully'}, context_instance=RequestContext(request))
else:
return render_to_response('inventory_app/add.html', {'form': form, 'add_form': add_form}, context_instance=RequestContext(request))
With an emphasis on the line before the add_form.is_valid line:
add_form = AddForm(request.GET)
if add_form.is_valid():
If you don't request the form then the .is_valid statement will always return false.
Upvotes: 2
Reputation: 11683
Try to find out the specific validation errors for your form, which will possibly give you more information about the specific error: http://docs.djangoproject.com/en/dev/topics/forms/#looping-over-the-form-s-fields
Upvotes: 0