我有三个相似的表单Form1包含多个echoicefields和带有按钮的BooleanFields。在views-function中,我希望在从每个字段中获取每个multipleechoicefield-choice并使用干净的数据后,将form从Form1(form=Form1)更改为Form2(form=Form2),并将Form3从Form2(Form3)更改为Form3。也就是说,模板有一个相同的表单,但是在三轮选择每个表单之后,表单/表单内容会发生变化。非常感谢任何帮助。在# in views.py
def function1(request,postID):
# get id for objcet in class Label
label = Label.objects.get(id=postID)
template = template1.html
if request.method == "POST":
form = Form1(request.POST, prefix="form1")
if form.is_valid and 'button' in request.POST:
# Fetch chosen field in Form1 with cleaned_data
a = form.cleaned_data['choices1']
# get the dict with all of the choices in formfield "choices1" in
# Form1 as keys
choices_dict = dict(form.fields['choices1'].choices)
# get the value for choice a in var a in choices_dict
i = choices_dict.get(a)
# Update the modelfield "labeled" in class Labeling in models.py
# with the string/value for a in var i
Labeling.objects.filter(labeled="").update(labeled=i)
# call function post_one_label for posting and saving in db
post_one_label(request, label)
# change form from Form1 to Form2
form = Form2(request.POST, prefix = "Form2")
# validate the changed form after pressing button with name button in
# the template
if form.is_valid() and 'button' in request.POST:
# Fetch chosen field in Form2, choices2 is MultipleChoiceField in Form2 alike choices1 in Form1, with cleaned_data
# call function post_one_label for posting and saving in db
post_one_label(request, label)
# Problem; the form changes from Form1 to Form2 but after submitting the cleaned_data for Form2 isn't processed, it returns None
#
b = form.cleaned_data['choices2']
else:
form = Form1()
# CSRF update
c = {'form':form, 'label':label }
c.update(csrf(request))
# return RequestContext - request for template template1.html
return render(request,template,c,context_instance=RequestContext(request))
def post_one_label(request, label):
label = request.POST.get('label',"")
oneLabel = Labeling(belongsTo=label, label=label)
oneLabel.save()
在表单.py在
^{pr2}$