【问题标题】:Flash Messages aren't appearing on pageFlash 消息未显示在页面上
【发布时间】:2020-11-25 05:26:17
【问题描述】:

我正在使用 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">&times;</span>
                      </button>
                  </div>
              {% endfor -%}
          {% endif %}
      {% endwith %}
  </div>
    {% block content %}
    {% endblock %}
  </div>
</body>

【问题讨论】:

  • 我的浏览器中显示了一个 KeyError 向我们展示整个错误消息。
  • 例如:我为 convert_from 输入了一个无效的输入,它显示:“werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: 浏览器(或代理)发送了一个此服务器无法理解的请求。KeyError: 'converting_from'"
  • 您的代码在到达 Flash 消息之前就崩溃了。
  • - 中的 {%- 是什么?

标签: python html flask flask-bootstrap


【解决方案1】:

您应该使用 Flask 中的 render_template 库,您的代码如下所示:

from flask import Flask, render_template

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:
        message = "{converting_from} is not a valid currency code!"
    
    try:
         c.get_symbol(converting_to)
    except KeyError:
        message = "{converting_to} is not a valid currency code!"

return render_template('base.html',message=message)

def check_valid_number():
    amount = request.form['amount']
    if amount.isnumeric() == True:
        amount= float(amount)
        return amount
    else:
        message = "Not a valid amount."

return render_template('base.html',message=message)

【讨论】:

  • 如果我使用内置的 flash() 函数,我应该不需要输入消息作为参数,虽然正确?
  • 另外,我在 app.py 中使用了 render_template,但我展示的代码来自我的 errors_check.py。运行这些检查后,我渲染了基本模板,但我的 flash 消息没有出现
猜你喜欢
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多