Retrieving Form data (from an A HTML page) in a python function pointing to a 'B' HTML page

I have an ‘A’ HTML page (URL: AppName/A) with an A_Form.
After submitting the A_form, the user shall go to the ‘B’ HTML page (URL:AppName/A/B) where other forms should pop up depending on the data of the A_Form submitted earlier.
After submitting the A_Form data, i want to get these data in the python parameter function pointing to the ‘B’ HTML page (URL:AppName/A/B).

In, ‘A’ HTML code, i have directed the data form to the ‘B’ HTML URL:
<form action=“A/B” method=“POST”>
{% csrf_token %}
{{ A_Form.as_p}}

In views.py, A HTLM python function code:
def Afunction_view(request):
AForm = A_Form()
if request.method == ‘POST’:
A_form_result = A_Form(request.POST)
BForm = B_Form()
CForm = C_Form()
return render(request, ‘AppName/B.html’{‘BForm’:BForm,‘CForm’:CForm})
else:
AForm = A_Form()
return render(request, ‘AppName/A.html’{‘AForm’:AForm})

In views.py, ‘B’ HTML python function code::
def Bfunction_view(request):
BForm = B_Form()
CForm = C_Form()
if request.method == ‘POST’:
else:
BForm = B_Form()
CForm = F_Form()
return render(request, ‘AppName/B.html’{‘BForm’:BForm,‘CForm’:CForm})

The problem with this approach is that after the form in the ‘A’ HTML page is submitted the user is directed to the ‘B’ HTML page and the B function is exectued with values on the request parameter… so is it more convenient to put all forms in the same page and using jquery to pop up the forms based on the data of the first form submitted or is there an easier way in django to make this ?

You should always redirect after a successful POST, rather than render a new form: https://en.wikipedia.org/wiki/Post/Redirect/Get

Therefore your A view should redircet to a B+C view.

ok but how do i keep my A form view data submitted in my A view in my B+C view because i will need it after in my B+C view?
Also,based on the A form data of my A view, the B+C view can be different

The easiest and most dependable way is to save it into the database. You can have a model that represents a submitted A form, and use its ID in a URL parameter for the B+C view.

Hello Adam,

Let 's take an example, after the user login, he will go to the home page and choose to go to the 'A’HTML page for instance. There, he will tick the fields of a form (that represent the countries form) that he wants to fill in the ‘B’ HTML page.These forms are all different so based on what he will tick the ‘B’ HTML page will be different and he will necessarily go from ‘A’ HTML page to the ‘B’ HTML page.
So, if I understand you well, after having ticked the ‘A’ form, I should save temporarily the ‘A’ form data in the database then redirect to the ‘B’ HTML page with an url that contain what has been ticked in the form and that will lead me to the ‘B’ HTML page whose the content will be based on what is on the URL?
If yes,shall i write the code that will link the url and the forms in the ‘B’ HTML file code or in the python function in views.py that is related to the ‘B’ HTML page?

Thank you for your help!

In general, what you want to do is redirect to the ‘B’ view passing the primary key of the ‘A’ data as a parameter. In your ‘B’ view, you get the ‘A’ object from the database to create your page / form.

1 Like

Thanks Ken, it worked !

See you later!
PJ

@adamchainz deserves the lion’s share of the credit here - I just jumped in at the end of this thread.

1 Like