【发布时间】:2020-04-17 20:41:35
【问题描述】:
Flask 的新手和学习做一些体面的事情的时间很短(大学)。 我有一项调查,我想使用 FLASH 来显示成功和错误消息(类别),虽然我无法这样做,但我对其进行了很好的研究,并且确实了解了它的工作原理,我的代码似乎与我找到的示例相比很好,但显然不是。如果有人可以帮助我,将不胜感激。
这是 .py 文件
def index():
form = Form()
if form.validate_on_submit():
flash(u'Thank you for your collaboration!', 'success')
if not form.validate_on_submit():
flash(u'Something went wrong!', 'error')
return redirect(url_for('index'))
return redirect(url_for('index'))
#submission_successful = True #or False. you can determine this.
return render_template('index1.html', form=form) #submission_successful=submission_successful)
.html 文件
<center>
<form methods='POST'>
{{ form.csrf_token }}
{{ form.hidden_tag() }}
<div style = font-size:18px; font-weight:bold; margin-left:200px; class="form-field"> {{ form.Email.label }} <br> {{ form.Email }} </div> <br>
<div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.sex.label }} {{ form.sex }} </div> <br>
<div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.age.label }} {{ form.age }} </div> <br>
<div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.Marital.label }} {{ form.Marital }} </div> <br>
<div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.county.label}} {{ form.county }} </div> <br>
<div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.Property.label }} {{ form.Property }} </div> <br>
<div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.PropertyStatus.label }} {{ form.PropertyStatus }} </div> <br>
<div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.Rooms.label }} {{ form.Rooms }} </div> <br>
<div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.People.label}} <br> {{ form.People }} </div> <br>
<div style = font-size:17px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.submit}} </div>
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
{% if category == 'success'%}
<div class='alert alert-success mb-3' role='alert' >
{{ message }}
</div>
{% endif %}
{% if category == 'error'%}
<div class='alert alert-danger mb-3' role='alert' >
{{ message }}
</div>
{% endif %}
{% endfor %}
{% endif %}
{% endwith %}
</center>
</form>
【问题讨论】:
标签: python-3.x flask web-applications visual-studio-code