提交表单后如何在 Flask 应用程序中显示确认模式?

How to show confirmation modal in Flask app after form submission?

我试图在我的 Flask 应用程序中向用户显示一条 confirmation/success 消息,但我不知道如何在模式中显示它。

@app.route("/", methods=["POST"]
def sendForm():
    form = ContactForm(request.form)
    if request.method == 'POST':
        if form.validate():
            # do stuff with form data
            return render_template("contact.html", form=form)
        else:
            # display error message
    else:
        return render_template("index.html")

我 return contact.html 模板的部分是我需要帮助的地方,我想。因为在 POST 请求成功完成后,该页面基本上会刷新并再次显示。需要以模式向用户显示确认消息。

在前端,我的表单是这样的:

<form method="POST" action="{{ url_for('sendForm') }}">
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
    {{ render_field(form.email) }}
    {{ render_field(form.name) }}
    <input id="submit-form" type="submit" value="Send">
</form>

我会做一些这样的事情...

在您的 render_template:

中传递一个布尔值
submission_successful = True #or False. you can determine this.
render_template("contact.html", form=form, submission_successful=submission_successful))

然后在您的模板中放置一个 if 语句

{% if submission_successful %}
    // modal elements here
{% endif %}