I have two formsets that I've displayed in one view. The second formset is not validating upon POST so it won't save to the database. None of the fields are required so there is no reason for it not to save.
If I load the page and just click submit without changing anything I receive 20 "This field is required". They load at the top of the page and don't tell me what fields seem to be missing. Extra credit if you can correct my {{ form.error }} tag so that the error shows up next to the form field instead of the top of the page. You can see in my code that I've tried multiple things and none work.
Here are my views:
class FamilyBaseFormSet(BaseModelFormSet):
def add_fields(self, form, index):
super(FamilyBaseFormSet, self).add_fields(form, index)
form.fields['state'].widget.attrs['class'] = 'input-mini'
form.fields['zip_code'].widget.attrs['class'] = 'input-small'
class FamilyMemberBaseFormSet(BaseModelFormSet):
def add_fields(self, form, index):
super(FamilyMemberBaseFormSet, self).add_fields(form, index)
form.fields['first_name'].widget.attrs['class'] = 'input-small'
form.fields['middle_name'].widget.attrs['class'] = 'input-mini'
form.fields['last_name'].widget.attrs['class'] = 'input-small'
form.fields['state'].widget.attrs['class'] = 'input-mini'
form.fields['zip_code'].widget.attrs['class'] = 'input-small'
def manage_family_member(request):
FamilyInlineFormSet = modelformset_factory(Family, extra=0, formset=FamilyBaseFormSet)
FamilyMemberInlineFormSet = modelformset_factory(FamilyMember,
extra=0, formset=FamilyMemberBaseFormSet)
if request.method == "POST":
family_formset = FamilyInlineFormSet(request.POST, request.FILES,
queryset=Family.objects.filter(id = '1'), prefix='f')
family_member_formset = FamilyMemberInlineFormSet(request.POST, request.FILES,
queryset=FamilyMember.objects.filter(family = '1'), prefix='fm')
if family_formset.is_valid() and family_member_formset.is_valid():
family_formset.save()
family_member_formset.save()
else:
family_formset = FamilyInlineFormSet(queryset=Family.objects.filter(id = '1'), prefix='f')
family_member_formset = FamilyMemberInlineFormSet(queryset=FamilyMember.objects.filter(family = '1'), prefix='fm')
context = RequestContext(request,{
'family_formset': family_formset,
'family_member_formset': family_member_formset,
})
return render_to_response("school/family/manage_family_members.html", context)
Here is the template:
{% extends 'school/base.html' %}
{% block title %}{{object_name}} Form{% endblock %}
{% block content %}
This is very useful when enrolling your children in their courses.<p>
{% for dict in family_member_formset.errors %}
{{ dict.errors }}
{% for error in dict.values %}
{{ error }}
{% endfor %}
{% endfor %}
<form method="post" action="" class="form-inline">
{{ family_formset.management_form }}
{% for form in family_formset.forms %}
<hr>
{{ form.id }}
<div class="input-prepend">
<span class="add-on">Name</span>
{{ form.error }}
{{ form.name }}
</div><br>
<div class="input-prepend">
<span class="add-on">Address 1</span>
{{ form.error }}
{{ form.address1 }}
</div><br>
<div class="input-prepend">
<span class="add-on">Address 2</span>
{{ form.error }}
{{ form.address2 }}
</div><br>
<div class="input-prepend">
<span class="add-on">City</span>
{{ form.error }}
{{ form.city }}
</div><br>
<div class="input-prepend">
<span class="add-on">State</span>
{{ form.error }}
{{ form.state }}
</div>
<div class="input-prepend">
<span class="add-on">Zip</span>
{{ form.error }}
{{ form.zip_code }}
</div><br>
<div class="input-prepend">
<span class="add-on">Email</span>
{{ form.error }}
{{ form.email_address }}
</div><br>
<div class="input-prepend">
<span class="add-on">phone_number</span>
{{ form.error }}
{{ form.phone_number }}
</div>
{% endfor %}
{{ family_member_formset.management_form }}
{% for form in family_member_formset.forms %}
<hr>
{{ form.id }}
{{ form.first_name.error }} {{ form.error }}
<div class="input-prepend">
<span class="add-on">First</span>
{{ form.first_name }}
</div>
{{ form.middle_name.error }}
<div class="input-prepend">
<span class="add-on">Middle</span>
{{ form.middle_name }}
</div>
{{ form.last_name.error }}
<div class="input-prepend">
<span class="add-on">Last</span>
{{ form.last_name }}
</div>
{{ form.address1_error }}
<div class="input-prepend">
<span class="add-on">Address 1</span>
{{ form.address1 }}
</div><br>
{{ form.address.error }}
<div class="input-prepend">
<span class="add-on">Address 2</span>
{{ form.address2 }}
</div><br>
{{ form.city.error }}
<div class="input-prepend">
<span class="add-on">City</span>
{{ form.city }}
</div><br>
{{ form.state.error }}
<div class="input-prepend">
<span class="add-on">State</span>
{{ form.state }}
</div>
{{ form.zip_code.error }}
<div class="input-prepend">
<span class="add-on">Zip</span>
{{ form.zip_code }}
</div><br>
{{ form.email_address.error }}
<div class="input-prepend">
<span class="add-on">Email</span>
{{ form.email_address }}
</div>
{% endfor %}
{% csrf_token %}
<input type='submit' value='Save Changes'>
</form>
{% endblock %}
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment