Django Submit Two Different Forms with One Submit Button

django submit two different forms with one submit button

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

Django multiple forms with one submit button

See - django submit two different forms with one submit button

Django's form class, when supplied data from the request, looks at POST or GET to find values by element id. All fields within the form must have unique id's or use the Django form's prefix keyword when constructing the form in your view.

In short - put all your form elements within a single html tag, use the prefix keyword (with unique prefixes for each Django form, any time the form is constructed in the view; this will alter the input/select/radiobox elements' id attribute ), and go from there. Supply the GET or POST to each form instance and

your good to go.

However, your code above shows you manually populating the id of each field. Rather, why not pass each form to the template as include as {{ form_name.as_p }} ? Unless you have a good reason to do otherwise, you are probably creating a headache for yourself.

Is it possible to create two different submit button in one form in Django?

This is possible, in your view you would do something like this:

class SomeView(UpdateView):

...

def form_valid(self, form):
self.object = form.save(commit=False)
if 'add-email' in self.request.POST:
self.object.name = f'{self.object.name}@gmail.com'

self.object.save()

Then in your template:

<form ...>
<button type="submit" name="save">Submit</button>
<button type="submit" name="add-email">Submit with Email</button>
</form>

Multiple Django Forms in Single View: Why Does One POST Clear Other Forms?

What I can understand is that you are passing request.POST with every form even the ones which aren't submitted. But in request.POST, you have the values for the form you submitted, not the other ones. Hence the error is showing field is required. I suggest you to do like this:

def update_machine_view(request, brand_name_slug, mclass_slug, profile_slug):

machineentry = MachineEntry.objects.select_related('plate', 'dimensions', 'chassis').get(profile_slug=profile_slug)

f_plate = PlateForm(instance=machineentry.plate)
f_dimensions = DimensionsForm(instance=machineentry.dimensions)
f_chassis = ChassisForm(instance=machineentry.chassis)

if request.method == 'POST':
if 'save_plate' in request.POST:
f_plate = PlateForm(request.POST, instance=machineentry.plate)
if f_plate.is_valid():
f_plate.save()

if 'save_dimensions' in request.POST:
f_dimensions = DimensionsForm(request.POST, instance=machineentry.dimensions)
if f_dimensions.is_valid():
f_dimensions.save()

if 'save_chassis' in request.POST:
f_chassis = ChassisForm(request.POST, instance=machineentry.chassis)
if f_chassis.is_valid():
f_chassis.save()

context = {
'f_plate': f_plate,
'f_dimensions': f_dimensions,
'f_chassis': f_chassis,
'obj': machineentry,
}

return render(request, "machines/update_machine_form.html", context)


Related Topics



Leave a reply



Submit