django modelformset_factory中的MultiValueDictKeyError [英] MultiValueDictKeyError in django modelformset_factory
问题描述
这是我的代码。 p>
forms.py
class SchoolSectionForm(forms.ModelForm):
def __init __(self,* args,** kwargs):
self.helper = FormHelper()
self.helper.form_tag = False
self .helper.layout =布局(
Div(
'name',
css_class ='name',
),
)
super(SchoolSectionForm,self).__ init __(* args,** kwargs)
class Meta:
model = SchoolSection
exclude = [
'学校',
'created_by',
]
class SectionBreakFormSet(BaseFormSet):
def __init __(self,* args, ** kwargs):
super(SectionBreakFormSet,self)._ _init __(* args,** kwargs)
class SectionBreakForm(forms.ModelForm):
def __init __(self,* args,** kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout =布局(
Div(
Div
'start_time',
css_class ='start_time',
),
Div(
'end_time',
css_class ='end_time',
),
css_class ='formset_element',
)
)
super(SectionBreakForm,self).__ init __(* args,** kwargs)
class Meta:
model = SectionBreak
exclude = [
'school_section',
'created_by',
]
views.py
def school_se ction_edit(request,section_id):
section = get_object_or_404(
SchoolSection,
id = section_id,
)
current_school = request。 user.current_school
school_sections_list = current_school.schoolsection_set.all()
section_break_formset = modelformset_factory(
SectionBreak,
max_num = 3,
extra = 0,
form = SectionBreakForm,
)
formset_qset = SectionBreak.objects.filter(school_section = section)
formset = section_break_formset(queryset = formset_qset)
school_section_form = SchoolSectionForm(instance = section)
如果request.method =='POST':
school_section_form = SchoolSectionForm(request.POST)
#此行中的错误提高
formset = section_break_formset(request.POST,queryset = formset_qset)
#此行中的错误引发
如果school_section_ form.is_valid()和formset.is_valid():
school_section_form.save()
formset.save()
messages.success(
请求,
u'xxx',
)
返回HttpResponseRedirect(reverse('school:school_section_add'))
else:
message.error(
请求,
u'xxx',
)
返回渲染(请求,'school / schoolsection_add.html',{
'school_section_form':school_section_form,
'formset':formset,
'school_sections_list':school_sections_list,
})
模板
< form class =new_section_formmethod =postaction = >
< div class =school_section_form>
{%crispy school_section_form%}
< / div>
< h3> Horarios de descanso:< / h3>
< div class =section_break_formset>
{%crispy formset formset.form.helper%}
< / div>
< button class =button color> guardar< / button>
< / form>
当我发布表单... crashh ....我有这个错误
感谢您的帮助...
异常类型:/ Administrador / ciclo-educativo / editar / 34 /
中的MultiValueDictKeyError异常值: 关键u'form-0-id未在<> QueryDict中找到:{u'name':[u'Primaria'],u'form-MAX_NUM_FORMS':[u'3'],u'form-TOTAL_FORMS ':[u'1'],u'form-0-start_time':[u'07:00:00'],u'form-0-end_time':[u'12:00:00'],u 'form-INITIAL_FORMS':[u'1'],u'csrfmiddlewaretoken':[u'aZkZPJ6tlzJeCd1kjC0EpkjPuFbWe6IB',u'aZkZPJ6tlzJeCd1kjC0EpkjPuFbWe6IB']}<>
您可能需要添加表单ID {{form.id }} 例如
{%crispy formset formset.form.id%}
I'm trying to imeplement an edit formset. Then, i'm instancing the objects in the formset using modelformset_factory. When the request isn't POST, the formset loads perfectly, but, if the request is POST, the formset constructor raises a MultiValueDictKeyError.
This is my code.
forms.py
class SchoolSectionForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
Div(
'name',
css_class='name',
),
)
super(SchoolSectionForm, self).__init__(*args, **kwargs)
class Meta:
model = SchoolSection
exclude = [
'school',
'created_by',
]
class SectionBreakFormSet(BaseFormSet):
def __init__(self, *args, **kwargs):
super(SectionBreakFormSet, self).__init__(*args, **kwargs)
class SectionBreakForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
Div(
Div(
'start_time',
css_class='start_time',
),
Div(
'end_time',
css_class='end_time',
),
css_class='formset_element',
)
)
super(SectionBreakForm, self).__init__(*args, **kwargs)
class Meta:
model = SectionBreak
exclude = [
'school_section',
'created_by',
]
views.py
def school_section_edit(request, section_id):
section = get_object_or_404(
SchoolSection,
id=section_id,
)
current_school = request.user.current_school
school_sections_list = current_school.schoolsection_set.all()
section_break_formset = modelformset_factory(
SectionBreak,
max_num=3,
extra=0,
form=SectionBreakForm,
)
formset_qset = SectionBreak.objects.filter(school_section=section)
formset = section_break_formset(queryset=formset_qset)
school_section_form = SchoolSectionForm(instance=section)
if request.method == 'POST':
school_section_form = SchoolSectionForm(request.POST)
# Bug raises in this line
formset = section_break_formset(request.POST, queryset=formset_qset)
# Bug raises in this line
if school_section_form.is_valid() and formset.is_valid():
school_section_form.save()
formset.save()
messages.success(
request,
u'xxx',
)
return HttpResponseRedirect(reverse('school:school_section_add'))
else:
messages.error(
request,
u'xxx',
)
return render(request, 'school/schoolsection_add.html', {
'school_section_form': school_section_form,
'formset': formset,
'school_sections_list': school_sections_list,
})
template
<form class="new_section_form" method="post" action="">
<div class="school_section_form">
{% crispy school_section_form %}
</div>
<h3>Horarios de descanso:</h3>
<div class="section_break_formset">
{% crispy formset formset.form.helper %}
</div>
<button class="button color">guardar</button>
</form>
When i post the form... crashh.... i have this error
thanks for help...
Exception Type: MultiValueDictKeyError at /administrador/ciclo-educativo/editar/34/ Exception Value: "Key u'form-0-id' not found in <>QueryDict: {u'name': [u'Primaria'], u'form-MAX_NUM_FORMS': [u'3'], u'form-TOTAL_FORMS': [u'1'], u'form-0-start_time': [u'07:00:00'], u'form-0-end_time': [u'12:00:00'], u'form-INITIAL_FORMS': [u'1'], u'csrfmiddlewaretoken': [u'aZkZPJ6tlzJeCd1kjC0EpkjPuFbWe6IB', u'aZkZPJ6tlzJeCd1kjC0EpkjPuFbWe6IB']}<>"
You might need to add the form id {{ form.id }} e.g.
{% crispy formset formset.form.id %}
这篇关于django modelformset_factory中的MultiValueDictKeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!