为了账号安全,请及时绑定邮箱和手机立即绑定

ValueError:在 Flask 的闪存中解压的值太多(预期为 2)

ValueError:在 Flask 的闪存中解压的值太多(预期为 2)

泛舟湖上清波郎朗 2023-02-07 16:42:30
我的应用程序中有ValueError (ValueError: too many values to unpack (expected 2))一个Flask。我开始知道这是因为我使用 flash 来显示带有类别的消息。问题发生了,当我尝试这个{% with messages = get_flashed_messages() %}  {% if messages %}    {% for category, msg in messages %}      <div class="alert {{category}}">        <h1>{{category}}:</h1>        <h4>{{msg}}</h4>      </div>    {% endfor %}  {% endif %}{% endwith %}但是,如果我只是从 for 循环中删除类别,它会起作用,但类别不会!{% with messages = get_flashed_messages() %}  {% if messages %}    {% for msg in messages %}      <div class="alert {{category}}">        <h1>{{category}}:</h1>        <h4>{{msg}}</h4>      </div>    {% endfor %}  {% endif %}{% endwith %}请帮我
查看完整描述

2 回答

?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

我猜你错过了在你的视图函数中设置第二个参数,消息的类别(它可以是error,warning无论如何..)flash()


flash(u'Invalid password provided', 'error')

不要忘记设置with_categories=true功能get_flashed_messages():


{% with messages = get_flashed_messages(with_categories=true) %} {# --HERE--  #}

  {% if messages %}

    {% for category, msg in messages %}


      <div class="alert {{category}}">

        <h1>{{category}}:</h1>

        <h4>{{msg}}</h4>

      </div>


    {% endfor %}

  {% endif %}

{% endwith %}


查看完整回答
反对 回复 2023-02-07
?
缥缈止盈

TA贡献2041条经验 获得超4个赞

我找到了该怎么做,问题是我不得不提到我想在神社中使用类别


{% with messages = get_flashed_messages( with_categories=true ) %}


{% with messages = get_flashed_messages(with_categories=true) %} 

  {% if messages %} 

     {% for category, msg in messages %}

        <div class="alert {{category}}">

          <h4>{{msg}}</h4>

        </div>

     {% endfor %}

  {% endif %}

{% endwith %}


查看完整回答
反对 回复 2023-02-07
  • 2 回答
  • 0 关注
  • 75 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信