我是Django的新手,拥有有限的Python经验。我正在尝试编辑表单和Formset。我可以编辑并保存表单,但是我无法在Formset中进行编辑以保存。这是我在Views中的代码:
@login_required
def edit_asr(request, asr_id):
context = RequestContext(request)
if not is_editor(request):
return HttpResponse("You are not authorized to view this record")
if request.method == 'POST':
asr_record = ASR.objects.get(id=asr_id)
form = BuildASRForm(request.POST, instance=asr_record)
formset = ASRTripLegFormset(request.POST, instance=asr_record)
if form.is_valid():
form.save(commit=True)
logger.info("ASR %s was edited by %s" % (asr_id, request.user))
return HttpResponseRedirect('/asr/%s/' % asr_id)
else:
logger.error("Edit ASR form returned the following errors for user %s: %s" % (request.user, form.errors))
else:
asr_record = ASR.objects.get(id=asr_id)
form = BuildASRForm(instance=asr_record)
formset = ASRTripLegFormset(instance=asr_record)
return render_to_response('asr/edit_asr.html', {'form': form, 'formset': formset, 'asr_id':asr_id}, context)
我已尝试在if formset.is_valid():
语句下缩进if form-is_valid():
次调用,但会抛出MultiValueDictKeyError
错误。对不起,如果这是一个新手问题,我就被困住了。
谢谢!