• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to handle two Django forms in one view?

Resolved: How to handle two Django forms in one view?

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have a view that contains two Django forms. They were previously working but now whichever one is underneath the top one does not work. I have tried switching the order of the forms and whichever form is on the top will work while the other one will not do anything when submitted.
I tried changing my forms so they resemble this example if request.method == "POST" and "selectgenderform" in request.POST: but that did not work (the forms did nothing).
Does anyone know how to fix this problem?
part of views.py
Currently, the bid_form is working because it is on the top, but if you flip the order the comment_form will work instead and the bid_form will not.
forms.py

Answer:

You have a flow problem. If the user submits a comment, then your bid_form is invalid, and so it goes to the ‘else’ section of the if bid_form.is_invalid(), which redirects to another page before the comment form is tested. Thankfully they both have the same else statement, so you can use elif rather than two else…ifs
Assuming only one form can be submitted at a time, try the following:
If both forms can be submitted at once and should both be handled, you will need to set a flag instead, eg:

If you have better answer, please add a comment about this, thank you!

django django-forms django-views python
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Watching state object in pinia doesn’t fire when object changes

22/03/2023

Resolved: Create From/To Pairs by sequential date R dplyr

22/03/2023

Resolved: Using a variable on next page before the if(isset statement

22/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.