how to submit two forms in django

  1. Create two forms in your Django app, for example:
class Form1(forms.Form):
    # form fields here

class Form2(forms.Form):
    # form fields here
  1. In your view, instantiate both forms:
def my_view(request):
    form1 = Form1()
    form2 = Form2()
    # other view logic
  1. Pass the forms to the template context:
return render(request, 'my_template.html', {'form1': form1, 'form2': form2})
  1. In your template, render both forms using the as_p method (or other appropriate method):
<form method="post">
    {% csrf_token %}
    {{ form1.as_p }}
    {{ form2.as_p }}
    <button type="submit">Submit</button>
</form>
  1. In your view, handle form submission:
def my_view(request):
    if request.method == 'POST':
        form1 = Form1(request.POST)
        form2 = Form2(request.POST)
        if form1.is_valid() and form2.is_valid():
            # process form data
        else:
            # handle form validation errors
    else:
        form1 = Form1()
        form2 = Form2()
  1. Update your template to display any form errors, if necessary:
<form method="post">
    {% csrf_token %}
    {{ form1.as_p }}
    {{ form1.non_field_errors }}
    {{ form2.as_p }}
    {{ form2.non_field_errors }}
    <button type="submit">Submit</button>
</form>