Je crée un jeu de formulaires avec django qui contient des informations sur les enfants. J'utilise createview (CBV) pour cela. Le formulaire s'affiche correctement, il a la fonctionnalité d'ajouter des enfants et de supprimer des enfants, tout fonctionne correctement. Mais, lorsque je clique sur submit, form_invalid
est appelé à la place de form_valid
. Pour connaître ce problème, j'ai imprimé form.errors
et j'ai vu les erreurs suivantes
<ul class="errorlist"><li>deal_id<ul class="errorlist"><li>This field is required.</li></ul></li><li>child_name<ul class="errorlist"><li>This field is required.</li></ul></li><li>son_or_daugher<ul class="errorlist"><li>This field is required.</li></ul></li><li>child_age<ul class="errorlist"><li>This field is required.</li></ul></li><li>child_education<ul class="errorlist"><li>This field is required.</li></ul></li><li>child_occupation<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
Voici mon code
Modèle :-
{% extends "forms_app/base.html" %}
{% load static %}
{% block title %}{% endblock %}
{% block content %}
<h2>Profile</h2>
<hr>
<div class="col-md-4">
<form action="" method="post">{% csrf_token %}
<table class="table">
{{ childrens.management_form }}
{% for form in childrens.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle row1 row2 %} formset_row">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<input type="submit" value="Submit"/> <a href="">back to the list</a>
</form>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'forms_app/jquery.formset.js' %}"></script>
<script type="text/javascript">
$('.formset_row').formset({
addText: 'Add Children',
deleteText: 'Remove',
prefix: 'familymember_set'
});
</script>
{% endblock %}
Afficher le code
class ChildrenView(CreateView):
template_name = 'forms_app/children_form.html'
form_class = ChildrenForm
success_url = reverse_lazy('forms_app:deal-entering')
# fields = ['name','class']
def get_context_data(self, **kwargs):
data = super(ChildrenView, self).get_context_data(**kwargs)
if self.request.POST:
data['childrens'] = ChildrenFormSet(self.request.POST)
else:
print('child form initial data')
initial_list = []
try:
print('its going on')
for i in range(self.request.session['children_count'] ):
print('still going on')
for field in self.fields:
initial_list.append(self.request.session[i + field])
data['childrens'] = ChildrenFormSet(initial = initial_list)
except:
data['childrens'] = ChildrenFormSet()
# data['childrens'].extra = self.request.session['children_count']
return data
def form_valid(self, form):
if form.is_valid():
count = 0
for f in form:
for field in ['deal_id','child_name','son_or_daugher','child_age','child_education','child_occupation']:
self.request.session[count + field] = f.cleaned_data[field]
count += 1
self.request.session['children_count'] = count
for i in range(count):
for field in ['deal_id','child_name','son_or_daugher','child_age','child_education','child_occupation']:
print(self.request.session[i + field])
for field in self.fields:
self.request.session[field] = form.cleaned_data[field]
self.request.session['valid_children'] = True
return super(ChildrenView, self).form_valid(form)
def form_invalid(self,form):
if form.is_valid():
pass
print(form.errors)
print('form invalid')
return super(ChildrenView, self).form_invalid(form)
Code du formulaire
class ChildrenForm(forms.ModelForm):
class Meta:
model = Children
fields = '__all__'
def clean(self):
print(self.cleaned_data)
ChildrenFormSet = modelformset_factory(Children,fields = '__all__',extra=1)
J'ai lu plusieurs messages sur StackOverflow. J'ai parcouru les documents de Django mais je ne parviens toujours pas à comprendre le problème. Veuillez m'aider.
Voici une capture d'écran de mon formulaire
Si vous avez un problème de compréhension, veuillez commenter, je vous répondrai très rapidement.