大家好,我回来了一个新的谜语。 我试图渲染一个表格和2个表格集,并这样做我使用前缀。 但我似乎不明白如何使用前缀。 结果是,当我尝试使用添加按钮向表单集添加更多表单时,我得到上面提到的错误。如果您可以为我提供解决方案,或者甚至向我展示一些提示,我会很高兴。
这是我的views.py(我不包括导入):
def get_researchnew(request):
change_crit = '0'
change_alt = '0'
CriterionFormSet = formset_factory(CreateCriterionForm,extra=1,can_delete=True)
AlternativeFormset = formset_factory(CreateAlternativeForm,extra=1,can_delete=True)
if request.method == 'POST':
if 'add_criterion' in request.POST:
post_data = request.POST.copy()
post_data['criterion_form-TOTAL_FORMS'] = int(post_data['criterion_form-TOTAL_FORMS']) + 1
research_form = CreateResearchForm(post_data,prefix='research_form')
criterion_formset = CriterionFormSet(post_data, request.FILES,prefix='criterion_form')
alternative_formset = AlternativeFormset(post_data,request.FILES,prefix='alternative_form')
change_crit ='1'
elif 'add_alternative' in request.POST:
post_data = request.POST.copy()
post_data['alternative_form-TOTAL_FORMS'] = int(post_data['alternative_form-TOTAL_FORMS']) + 1
research_form = CreateResearchForm(post_data,prefix='research_form')
criterion_formset = CriterionFormSet(post_data, request.FILES,prefix='criterion_form')
alternative_formset = AlternativeFormset(post_data,request.FILES,prefix='alternative_form')
change_alt = '1'
elif 'submit' in request.POST:
research_form = CreateResearchForm(request.POST,prefix='research_form')
criterion_formset = CriterionFormSet(request.POST, request.FILES,prefix='criterion_form')
alternative_formset = AlternativeFormset(request.POST,request.FILES,prefix='alternative_form')
if research_form.is_valid() and criterion_formset.is_valid() and alternative_formset.is_valid():
research_form.save()
research_instance = research.objects.last()
for form in criterion_formset:
if form.is_valid():
instance = form.save(commit=False)
instance.criterion_research = research_instance
instance.save()
for form in alternative_formset:
instance = form.save(commit=False)
instance.alternative_research = research_instance
instance.ranking = '1'
instance.save()
return HttpResponse('Research successfully added to database.')
else:
research_form = CreateResearchForm(prefix='research_form')
criterion_formset = CriterionFormSet(prefix='criterion_forms')
alternative_formset = AlternativeFormset(prefix='alternative_form')
return render(request,'CreateResearchnew.html',{
'research_form':research_form,
'criterion_formset':criterion_formset,
'alternative_formset':alternative_formset,
'change_crit':change_crit,
'change_alt':change_alt,
})
CreateResearchnew.html:
<form method="post">
{% csrf_token %}
<table>
{% for field in research_form %}
<th>
{{ field.errors }}
{{ field.label_tag }}
<br>
{{ field }}
</th>
{% endfor %}
</table>
{{ criterion_formset.management_form }}
<table>
{% for criterion_form in criterion_formset %}
<tr>
{% for field in criterion_form %}
<th>
{% if change_crit != '1' %}
{{ field.errors }}
{% endif %}
{{ field.label_tag }}
<br>
{{ field }}
</th>
{% endfor %}
</tr>
{% endfor %}
</table>
<input title='Add a new Criterion' type='submit' name='add_criterion' value='Add' />
{{ alternative_formset.management_form }}
<table>
{% for alternative_form in alternative_formset %}
<tr>
{% for field in alternative_form %}
<th>
{% if change_alt != '1' %}
{{ field.errors }}
{% endif %}
{{ field.label_tag }}
<br>
{{ field }}
</th>
{% endfor %}
</tr>
{% endfor %}
</table>
<input title='Add a new Alternative' type='submit' name='add_alternative' value='Add' />
<input title='Submit' type='submit' name='submit' value='Submit' />
</form>
回溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.2:8888/utastar/get_research
Django Version: 1.10.5
Python Version: 2.7.13
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'utastar']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\Flokos\Anaconda2\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "C:\Users\Flokos\Anaconda2\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Flokos\Anaconda2\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Flokos\Documents\mysite\utastar\views.py" in get_researchnew
87. post_data['criterion_form-TOTAL_FORMS'] = int(post_data['criterion_form-TOTAL_FORMS']) + 1
File "C:\Users\Flokos\Anaconda2\lib\site-packages\django\utils\datastructures.py" in __getitem__
85. raise MultiValueDictKeyError(repr(key))
Exception Type: MultiValueDictKeyError at /utastar/get_research
Exception Value: "'criterion_form-TOTAL_FORMS'"