I try to use 2 WT quickforms on one page (AdminRightsForm and RevokeAdminRightsForm). I try to create 2 forms for an Admin page, one to grant admin rights/one to revoke admin rights to certain users (based on emailaddress). It seems like validate_on_submit is executed for both forms, although only one of the forms gets submitted (either first or second button got clicked to submit). I have no clue what I did wrong and how to resolve this issue. If I have only one form on the page then it works correctly, adding the second form messes up everything. What did I miss? I appreciate any guidance.
``
@app.route("/admin", methods=["GET", "POST"])
@admin_only
def admin_settings():
admins = db.session.execute(db.select(User).filter_by(is_admin=True)).scalars().all()
add_form = AdminRightsForm()
revoke_form = RevokeAdminRightsForm()
if add_form.validate_on_submit():
user_to_admin = db.session.execute(db.select(User).filter_by(email=add_form.email.data)).scalar()
print(user_to_admin)
if user_to_admin:
print("user to admin")
user_to_admin.is_admin = True
db.session.commit()
#return redirect(url_for("admin_settings"))
else:
flash("Email does not exit. Check email address")
#return redirect(url_for("admin_settings"))
if revoke_form.validate_on_submit():
user_off_admin = db.session.execute(db.select(User).filter_by(email=revoke_form.email.data)).scalar()
if user_off_admin:
print("user OFF admin")
user_off_admin.is_admin = False
db.session.commit()
#return redirect(url_for("admin_settings"))
else:
flash("Email does not exit. Check email address")
#return redirect(url_for("admin_settings"))
return render_template("admin.html", add_form=add_form, revoke_form=revoke_form, logged_in=current_user.is_authenticated, admins=admins)
``
** WT Form Classes:**
class AdminRightsForm(FlaskForm):
email = EmailField("Type the Email Address to grant admin rights", validators=[DataRequired()])
submit_grant = SubmitField("Grant Admin Rights!")
class RevokeAdminRightsForm(FlaskForm):
email = EmailField("Type the Email Address to revoke admin rights", validators=[DataRequired()])
submit_revoke = SubmitField("Revoke Admin Rights!")
HTML
{{ wtf.quick_form(add_form, button_map={"submit_grant": "primary"}) }}
<p></p>
{{ wtf.quick_form(revoke_form, button_map={"submit_revoke": "danger"}) }}
<p></p>
I tried to create 2 independent forms to grant/revoke admin rights on the same page, but when I submit (click submit button) the first form, the second one also gets validated/submitted (print("user OFF admin")line gets executed). If I submit the second form, the first one also gets submitted (print("user to admin")line gets executed). How can I get only the one that was clicked get submitted and the relevant code after form.validate_on_submit executed?