Search code examples
pythonhtmlformsflask

How to create multiple Submit buttons with Flask forms?


Problem : I want to do the whole thing with the Form_Setting class in the forms.py file. Here I have defined the 4 submit buttons. I then use the buttons in the settings.html. In the routes.py file I want to react when they are pressed. However, at the moment it is still so that I can react with form.validate_on_submit() only generally on the submit. How can I distinguish which of the submit buttons were pressed to then perform different actions?

routes.py

from page import app
from flask import render_template, redirect, url_for
from page.models import Settings
from page.forms import Form_Setting
from page import db



@app.route('/settings', methods=['GET','POST'] )
def Settings_Page():

    form = Form_Setting()

    if form.validate_on_submit():
      save_email =  Settings(email_adress=form.email_adress.data) 
      db.session.add(save_email)
      db.session.commit()
      return redirect(url_for('Settings_Page'))
    
    return render_template('settings.html',form=form)

forms.py

from flask_wtf import FlaskForm 
from wtforms import StringField, SubmitField

class Form_Setting(FlaskForm):
    email_adress = StringField(label='Email')
    dooralarm_time = StringField(label='Türalarm')
    minimum_temp = StringField(label='MinTemp')
    maximum_temp = SubmitField(label='MaxTemp')


    submit_email = SubmitField(label='Eingabe')
    submit_door = SubmitField(label='Eingabe')
    submit_temp_max = SubmitField(label='Eingabe')
    submit_temp_min = SubmitField(label='Eingabe')

settings.html

{%extends 'base.html' %}

{% block ActivSettings%}
    active
{% endblock %}

{% block css %}
    <link rel="stylesheet" type="text/css" href="{{url_for('static',filename='css/style.css')}}"/>
{% endblock %}


{% block title %}
    Einstellungen
{% endblock %}

{% block content %}
    <div>
        <div class="container">
            <h1 class="setting-page">Einstellungen</h1>
            <!--Email Adresse-->
            <div class="row ">  
                <div class="col-sm-12">
                    <div class="d-flex justify-content-start align-items-center" style="height: 8vh;"> 
                        <div class="col-sm-3 ">
                            <h3 class="setting-label">E-mail</h3>
                        </div>

                        <div class="col-sm-9">
                            <form  method="POST" class="form-register flex-fill d-flex" >
                                <!--Schutz gegen CSRF-->
                                {{ form.hidden_tag() }}
                                {{ form.email_adress(class="form-control", placeholder="E-Mail") }}
                                {{ form.submit_email(class="btn btn-md btn-block btn-primary", style="background-color: #6941c6; border:none; color: #f9f5ff; margin-left: 1vw;" )}}
                            </form>
                        </div>
                    </div>
                    <hr class="divider">
                </div>  
            </div>

            <!--Türalarm Zeitintervall-->
            <div class="row ">  
                <div class="col-sm-12">
                    <div class="d-flex justify-content-start align-items-center" style="height: 8vh;"> 
                        <div class="col-sm-3 ">
                            <h3 class="setting-label">Türalarm Zeitintervall</h3>
                        </div>

                        <div class="col-sm-9">
                            <form  method="POST" class="form-register flex-fill d-flex" >
                                <!--Schutz gegen CSRF-->
                                {{ form.hidden_tag() }}
                                {{ form.dooralarm_time(class="form-control", placeholder="Zeitintervall") }}
                                {{ form.submit_door(class="btn btn-md btn-block btn-primary", style="background-color: #6941c6; border:none; color: #f9f5ff; margin-left: 1vw;" )}}
                            </form>
                        </div>
                    </div>
                    <hr class="divider">
                </div>  
            </div>

            <!--Temp. Schwellwert Minimal-->
            <div class="row ">  
                <div class="col-sm-12">
                    <div class="d-flex justify-content-start align-items-center" style="height: 8vh;"> 
                        <div class="col-sm-3 ">
                            <h3 class="setting-label">Temp. Schwellwert Minimal</h3>
                        </div>

                        <div class="col-sm-9">
                            <form  method="POST" class="form-register flex-fill d-flex" >
                                <!--Schutz gegen CSRF-->
                                {{ form.hidden_tag() }}
                                {{ form.dooralarm_time(class="form-control", placeholder="Temp. Minimal") }}
                                {{ form.submit_temp_max(class="btn btn-md btn-block btn-primary", style="background-color: #6941c6; border:none; color: #f9f5ff; margin-left: 1vw;" )}}                      
                            </form>
                        </div>
                    </div>
                    <hr class="divider">
                </div>  
            </div>

            <!--Temp. Schwellwert Maximal-->
            <div class="row ">  
                <div class="col-sm-12">
                    <div class="d-flex justify-content-start align-items-center" style="height: 8vh;"> 
                        <div class="col-sm-3 ">
                            <h3 class="setting-label">Temp. Schwellwert Maximal</h3>
                        </div>

                        <div class="col-sm-9">
                            <form  method="POST" class="form-register flex-fill d-flex" >
                                <!--Schutz gegen CSRF-->
                                {{ form.hidden_tag() }}
                                {{ form.dooralarm_time(class="form-control", placeholder="Temp. Maximal") }}
                                {{ form.submit_temp_min(class="btn btn-md btn-block btn-primary", style="background-color: #6941c6; border:none; color: #f9f5ff; margin-left: 1vw;" )}}                                                
                            </form>
                        </div>
                    </div>
                    <hr class="divider">
                </div>  
            </div>
            
        </div>
    </div>
    
{% endblock %}

Webseite The result for the page


Solution

  • You can iterate through request.form using the variable name of the submit button.

    from flask import request
    
    if 'submit_email' in request.form:
        # do something
    elif 'submit_door' in request.form:
        # do something
    

    Be sure that the form will still validate properly form.validate_on_submit()