How to get the name of the submitted form in Flask?

I create a site using Flask, and on one page I have two forms. If there is a POST, I need to decide which form is being submitted. I can, of course, subtract it from the fields present in request.form , but I would rather make it explicit by getting the name (defined by <form name="my_form"> ) of the form that was submitted. I tried several things, for example:

 @app.route('/myforms', methods=['GET', 'POST']) def myForms(): if request.method == 'POST': print request.form.name print request.form.['name'] 

but unfortunately nothing works. Does anyone know where I can get the name of the submitted form? All tips are welcome!

+5
source share
1 answer

No form name. This information is not sent by the browser; the name attribute in the <form> tags is intended to be used exclusively on the browser side (and is deprecated for download, use id instead).

You can add this information using a hidden field, but the most common way to distinguish form postings from the same form handler is to give the submit button a name:

 <submit name="form1" value="Submit!"/> 

and

 if 'form1' in request.form: 

but you can also use the <input type="hidden"> field to enable tools to distinguish between forms.

+11
source

Source: https://habr.com/ru/post/1204108/


All Articles