如何迭代django中的formset?

时间:2011-11-10 20:58:56

标签: django

这是我的views.py:

# Create your views here.
def codepost(request):
if request.method == 'POST':
    form=CodeFormSet(request.POST)
    if form.is_valid():
        datan = ""
        for forms in form.ordered_forms:
            data = forms.cleaned_data['code']   
            datan = datan + data
        return render_to_response('submissiondone.html', {'data':datan})
else:
    form = CodeFormSet()
    data1=QuestionBase.objects.get(pk=1)
    #form.append(data1.text)
    #data1 = mform.text
    csrfContext = RequestContext(request)
return render_to_response('quesdisp.html', {'form': form})

这给出了我不想要的附加ORDER字段。那么,我如何迭代一个formset?如果我从formset中删除can_order = true,那么它不会将“code”识别为有效输入。

因此,我如何迭代这个?

编辑: 这是我的forms.py中最糟糕的部分:

from django import forms
from django.forms.formsets import formset_factory

class CodeForm(forms.Form):
    code = forms.CharField(widget = forms.Textarea)


CodeFormSet = formset_factory(CodeForm, extra = 5, )

1 个答案:

答案 0 :(得分:6)

formset = CodeFormSet(request.POST)
for form in formset:
    form.cleaned_data...