📜  如何在 django 中提交两个表单 - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:47.348000             🧑  作者: Mango

代码示例1
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")