python - Flask:忽略 'category' 参数的 Flash 函数

标签 python flask jinja2 flash-message

所以我写了 Controller :

@app.route('/')
def index():
    flash('Hello world!', 'success')
    return render_template('index.html')

然后在我的模板中输出这样的 flash 消息:

{%- with messages = get_flashed_messages(with_categories=true) -%}
{%- if messages -%}
    <ul class="flashes unstyled">
    {%- for category, message in messages -%}
        <li class="alert alert-{{ category }}">
            <a class="close" data-dismiss="alert">&times;</a>
            {{ message }}
        </li>
    {%- endfor -%}
    </ul>
{%- endif -%}
{%- endwith %}

但问题是我总是得到“消息”类别,所以 <li>上课'alert alert-message' .
我阅读了文档,对我来说我做的一切都是对的,但是 'flash'函数忽略第二个参数并始终使用默认值 'message' (而不是我给的“成功”)。

我想知道是否有人遇到过这个问题并且知道如何处理它?<​​/p>

最佳答案

编辑:根据其他评论,无需测试 kwarg。

基于 http://flask.pocoo.org/docs/api/#message-flashing 上的文档看来您需要使用这种格式。 flash(消息, category='message')

@app.route('/')
def index():
    flash('Hello world!', category='success')
    return render_template('index.html')

关于python - Flask:忽略 'category' 参数的 Flash 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18396936/

相关文章:

dynamic - Jinja 动态嵌套变量

python - 美化 Jinja2 模板

python - seaborn boxplot 和 stripplot 点未按色调在 x 轴上对齐

python - gevent + Flask 似乎阻塞

python - 无法使用flask_sqlalchemy获得一对多关系,得到sqlalchemy.exc.InvalidRequestError

flask - 从 wtforms 和 flask-sqlalchemy 中的数据库查询中获取选择

python - 使用 Jinja2 构建链接列表

python - Pygame:游戏结束屏幕理论上不显示,它应该

jquery - Django 模板使用带有 {{}} 的 jquery 脚本

python - 来自 df 的字典,键内有列