简体   繁体   English

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

[英]How do i submit mutiple crispy forms in django using one submit button?

I have two separate forms which i am trying to render in a single template. 我尝试在一个模板中呈现两种单独的形式。 However when i am trying to submit the form, the form is not getting submitted. 但是,当我尝试提交表单时,表单未提交。 Is there any possible way to do this? 有什么可行的方法吗? It is working fine when i am not using crispy forms 当我不使用酥脆形式时,它工作正常

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 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>

If you check/inspect your HTML code in the browser, you'd see two form tags nested under an outer form tag (the one added by you). 如果您检查/在浏览器中检查你的HTML代码,你会看到两个form外嵌套在标签form标签(一个由您添加)。

You need to avoid this situation by adding 您需要通过添加来避免这种情况

helper.form_tag = False

to helper objects under each forms. 在每种形式下帮助对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM