根据MultipleChoiceFields(Django)中的选项更改视图函数中的表单

时间:2015-10-08 13:55:23

标签: python django django-forms django-templates django-views

我有三种形式,包括Form1,包含MultipleChoiceFields,以及BooleanFields和Buttons。在views -function中,我希望在获取每个MultipleChoicefield之后,分别从Form1(form = Form1)更改Form2(form = Form2)和Form3(Form3)中的Form3 - 从每个字段中选择cleaning_data。也就是说,模板具有一种相同的形式,但是在三轮中选择每一种形式之后改变形式/形式内容。任何帮助是极大的赞赏。

# 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()

在forms.py

class Form1(forms.Form):

   Button_A = forms.BooleanField(initial=False, required=False)
   Button_B = forms.BooleanField(initial=False, required=False) 





    choices_group1 = (('1','A'),
                 ('2','B'),
                 ('3','C'),

          )


    choices1 = forms.MultipleChoiceField(choices=choices_group1,required=False)



    class Meta:
         model = Labeling # The class that I'm using, the same for all of the forms
         fields = ('choices1') # same as the field choices1 in the class Labeling

   # init and cleaned_data function   

    def __init__(self, *args, **kwargs):
        super(Form1, self).__init__(*args, **kwargs)



        def clean(self,*args, **kwargs):
            cleaned_data = super(Form1, self).clean(*args, **kwargs)

            return cleaned_data


in template.html

    <form  action="" method="post">{% csrf_token %}
        {{  form.as_p }}

         <input type="submit" name="button"  value="Submit"></input>

   </form> 

1 个答案:

答案 0 :(得分:0)

感谢Ian的推荐,我成功使用了SessionWizard。这是一个很好的教程,如果其他人像我一样遇到这个教程:Video tutorial on Form Wizard (Sessionwizard) in Django