如何使用一个提交按钮在Django中提交多个脆皮表格?

时间:2019-07-03 07:24:36

标签: python django

我尝试在一个模板中呈现两种单独的形式。 但是,当我尝试提交表单时,表单未提交。 有什么可行的方法吗?当我不使用酥脆的表格时,效果很好

forms.py

class BasicInfoForm(ModelForm):
    class Meta:
        model = BasicInfo
        fields = '__all__'

    helper = FormHelper()
    helper.form_class = 'form-group'
    helper.layout = Layout(
                        Row(
                            Column('name', css_class='form-group col-sm-4 col-md-4'),
                            ),
                        Row(
                            Column('birthMonth',css_class='form-group col-sm-4 col-md-4'),
                            ),
                        Row(
                            Column('birthYear', css_class='form-group col-sm-4 col-md-4'),
                            ),
                        )

class IncomeDetailForm(ModelForm):
    class Meta:
        model = IncomeDetail
        fields = '__all__'
        exclude = ['user']

    helper = FormHelper()
    helper.form_class = 'form-group'
    helper.layout = Layout(
                        Row(
                            Column('gross', css_class='form-group col-sm-4 col-md-4'),
                            Column('monthlyExpense',css_class='form-group col-sm-4 col-md-4'),
                            Column('hasSavingsInvestment', css_class='form-group col-sm-4 col-md-4'),
                            ))

Views.py

def getIndexDetails(request):
        if request.method == 'POST':
        print("inside post method")
        basicinfoform = BasicInfoForm(request.POST)
        if basicinfoform.is_valid():
            basicinfoform.save()
            incomedetailform= IncomeDetailForm(request.POST)
        if incomedetailform.is_valid():
            incomedetailform.save()



<form action="." method="POST">
        {% csrf_token %}
        {{ crispy basicinfoform }}
        {% crispy incomedetailform %}
        <input type="submit" class="btn btn-info" value="Submit Button">                 
      </form>

1 个答案:

答案 0 :(得分:0)

如果在浏览器中检查/检查HTML代码,则会看到两个form标签嵌套在外部form标签下(由您添加的标签)。

您需要通过添加

来避免这种情况
helper.form_tag = False

在每种形式下帮助对象。