gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:14 25 4
gpt4 key购买 nike

我试图在 Flask 应用程序中向用户显示确认/成功消息,但我不知道如何在模式中显示它。

@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")

我认为,我返回 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 中传递一个 bool 值:

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 %}

关于python - 提交表单后如何在 Flask 应用程序中显示确认模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36872257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com