Eric
Eric

Reputation: 368

Multiple Forms on 1 Page Python FLASK

So I am loading all users from a database on a single page and I'm generating a password reset form for each user on the same page rather than having an individual page for each user.

My question is how can I click submit and apply the change for that specific user since I have multiple forms and submit buttons for each user via drop-down menu?

In my case the submit button is the "Reset Password" button.

enter image description here

I'm trying to call the form normally using

if request.method == "POST" and form.validate():
    password = request.form['password']

but I'm getting exception error

name 'form' is not defined

I've been trying to solve this for a while but i'm getting pretty confused now as I've got multiple forms (one per user) on the same page.

NOTE : I'm not using WTForms for this task

Thanks

Upvotes: 1

Views: 7183

Answers (1)

EThomas
EThomas

Reputation: 31

I'm not sure if this one has been answered but here is what i just figured out:

After your standard

if request.method == 'POST':

you can test for the existence of each form item within the request.form data. So add another if statement after the first

if 'my_form_element_name' in request.form:
    print ('stuff')

If you have other types of form data such as files, you can do something like:

if request.method == 'POST':
    if 'file_element_name' in request.files:
        return stuff
    elif 'my_form_element_name' in request.form:
           return stuff
    else: return stuff
else: return stuff

I have four forms in one html file and this method worked for me.

Upvotes: 3

Related Questions