简体   繁体   中英

flask-wtforms show a popup after clicking on submit button

I am using flask wtform and i want to show a confirmation popup after pressing the submit button so user can see that their submission is successful.

forms.py:

    post = FloatField('', validators=[DataRequired()])
    submit = SubmitField('Quote')

.html:

<form action="{{ url_for('product', id=product.id) }}" method="post">
    {{ form.csrf_token }}
    <h4>
        {{ form.post(class='input-text qty', maxlength='9', placeholder='Price') }}
        {{ form.submit(class='button primary-btn', id='orderform') }}
    </h4>
</form>
.
.
.
.
.
<script>
    $(document).ready(function(){
        $("orderform").submit(function(){
             alert("Submitted");
        });
    });
</script>

When i press the submit button the value is added to the database but i don't get any confirmation popup.

Flask has a built-in functionality called Flash , it should be able to provide a feedback for your application, you just have to import this on your routes.py:

from flask import flash

Then, also on your routes.py file, when you want to call the message, just call the flash method:

flash("Your submission is successful")

And finally, at your.html, allow the page to receive the flashing messages:

        {% with messages = get_flashed_messages() %}
            {% if messages %}
                {% for msgs in messages %}
                    <div class="alert alert-info" role="alert">
                        {{msgs}}
                    </div>
                {% endfor %}
            {% endif %}
        {% endwith %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM