django submit two different forms with one submit button

2025-03-17 09:07:00
admin
原创
59
摘要:问题描述:is it possible to submit two different forms, with one submit button in django?i have one form called "instrument" and 4 equal forms "config". now i'd...

问题描述:

is it possible to submit two different forms, with one submit button in django?
i have one form called "instrument" and 4 equal forms "config". now i'd like to submit always one config and instrument. e.g. instrument + config 1, and instrument + config 2. and every config have its own submit button.

i have tried it with one button in the config form:

<input onclick="submitForms()" class="btn btn-primary cfg" type="submit" value="Start" >

and call a js function 'onclick':

submitForms = function(){
    console.log('ok'); //only for testing
    document.forms["firstForm"].submit();
    document.forms["secondForm"].submit();
}

this is my method in the views.py:

if request.method == 'POST':
        form1 = dataproviderInstrumentForm(request.POST)
        form2 = dynamicTimeseriesForm(request.POST)
        print(request.POST)
        if form1.is_valid() or form2.is_valid(): 
            # do some stuff

else:
    form1 = dataproviderInstrumentForm() # an unbound form
    form2 = dynamicTimeseriesForm() # an unbound form

解决方案 1:

Instead of having multiple <form ..> tags in html, use only one <form> tag and add fields of all forms under it.

Example in template

<form >
    {{ form1.as_p }}
    {{ form2.as_p }}
    {{ form3.as_p }}
</form>

So when user submits the form you will get all forms data in view, then you can do what you are doing in view. As

if request.method == 'POST':
        form1 = Form1(request.POST)
        form2 = Form2(request.POST)
        print(request.POST)
        if form1.is_valid() or form2.is_valid(): 

Its better to use form prefix in such cases.

So you can do

if request.method == 'POST':
        form1 = Form1( request.POST,prefix="form1")
        form2 = Form2( request.POST,prefix="form2")
        print(request.POST)
        if form1.is_valid() or form2.is_valid(): 
else:
        form1 = Form1(prefix="form1")
        form2 = Form2(prefix="form2")

解决方案 2:

Extending the @Rohan answer and adding more control on forms.

Not dependent forms/Without relationship/Save any form from multiple forms

Check individually each form to check which form are not valid. Then store them into context if contain errors or redirect them.

if request.method == 'POST':
    form1 = Form1( request.POST,prefix="form1")
    form2 = Form2( request.POST,prefix="form2")
    
    if form1.is_valid():
       # save them    
       
       # context['form1_message'] = 'Form1 saved'
    else: 
       #save them into context
       context['form1']= form1
    
    if form2.is_valid():
       # save them    
       # context['form2_message'] = 'Form2 saved'
    else: 
       #save them into context
       context['form2']= form2

    if form1.is_valid() and  form2.is_valid(): 
       #that's mean both form is valid and saved successfully 
       return redirect('page')
    else:
        return render('/page', context)


else:
    form1 = Form1(prefix="form1")
    form2 = Form2(prefix="form2")

Dependent forms/Modelform(1-1,1-m)/Relationship form

One Parent form and one child form that depends on Parent form. if both forms are saved or checked errors at same time then we will use this method.

if request.method == 'POST':
    form1 = Form1( request.POST,prefix="form1")
    form2 = Form2( request.POST,prefix="form2")
    
    if not form1.is_valid():
       #save them into context
       context['form1']= form1
    
    if not form2.is_valid():
       #save them into context
       context['form2']= form2

    if form1.is_valid() and  form2.is_valid(): 
       #that's mean both form is valid and saved successfully 
       return redirect('page')
    else:
        return render('/page', context)


else:
    form1 = Form1(prefix="form1")
    form2 = Form2(prefix="form2")

解决方案 3:

i didnt get much luck with the above but ive been using crispy forms. So the way i did it finally i had different forms but in crispy forms i set the self.helper.form_tag = False, that way i only carry the Html form tags and the rest eventhough are forms and i can specify it in html i can submit all data together

相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   2482  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1533  
  PLM(产品生命周期管理)项目对于企业优化产品研发流程、提升产品质量以及增强市场竞争力具有至关重要的意义。然而,在项目推进过程中,范围蔓延是一个常见且棘手的问题,它可能导致项目进度延迟、成本超支以及质量下降等一系列不良后果。因此,有效避免PLM项目范围蔓延成为项目成功的关键因素之一。以下将详细阐述三大管控策略,助力企业...
plm系统   0  
  PLM(产品生命周期管理)项目管理在企业产品研发与管理过程中扮演着至关重要的角色。随着市场竞争的加剧和产品复杂度的提升,PLM项目面临着诸多风险。准确量化风险优先级并采取有效措施应对,是确保项目成功的关键。五维评估矩阵作为一种有效的风险评估工具,能帮助项目管理者全面、系统地评估风险,为决策提供有力支持。五维评估矩阵概述...
免费plm软件   0  
  引言PLM(产品生命周期管理)开发流程对于企业产品的全生命周期管控至关重要。它涵盖了从产品概念设计到退役的各个阶段,直接影响着产品质量、开发周期以及企业的市场竞争力。在当今快速发展的科技环境下,客户对产品质量的要求日益提高,市场竞争也愈发激烈,这就使得优化PLM开发流程成为企业的必然选择。缺陷管理工具和六西格玛方法作为...
plm产品全生命周期管理   0  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用