我正在使用 python 和烧瓶创建一个货币转换器,并尝试为无效输入闪烁错误消息。表单在提交时会自动路由到错误检查。我一直在操纵输入以故意引发错误,并且 KeyError 显示在我的浏览器中,但没有在我的页面上闪烁。
这些是我正在尝试做的检查:
def check_valid_code():
converting_from = (request.form['converting_from']).upper()
converting_to = (request.form['converting_to']).upper()
c= CurrencyCodes()
try:
c.get_symbol(converting_from)
except KeyError:
flash("{converting_from} is not a valid currency code!")
try:
c.get_symbol(converting_to)
except KeyError:
flash("{converting_to} is not a valid currency code!")
def check_valid_number():
amount = request.form['amount']
if amount.isnumeric() == True:
amount= float(amount)
return amount
else:
flash("Not a valid amount.")
我的 base.html(使用 Bootstrap)应该出现消息:
<body>
<div class="container" style="min-height:100% width:80%">
<div class="col-sm-4">
{% with errors = get_flashed_messages(category_filter=["danger"]) %}
{% if errors %}
{%- for message in errors %}
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<span>{{ message }}</span>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endfor -%}
{% endif %}
{% endwith %}
</div>
{% block content %}
{% endblock %}
</div>
</body>