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

我的应用程序中有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 %}

请帮我


泛舟湖上清波郎朗
浏览 136回答 2
2回答

至尊宝的传说

我猜你错过了在你的视图函数中设置第二个参数,消息的类别(它可以是error,warning无论如何..)flash()flash(u'Invalid password provided', 'error')不要忘记设置with_categories=true功能get_flashed_messages():{% with messages = get_flashed_messages(with_categories=true) %} {# --HERE--&nbsp; #}&nbsp; {% if messages %}&nbsp; &nbsp; {% for category, msg in messages %}&nbsp; &nbsp; &nbsp; <div class="alert {{category}}">&nbsp; &nbsp; &nbsp; &nbsp; <h1>{{category}}:</h1>&nbsp; &nbsp; &nbsp; &nbsp; <h4>{{msg}}</h4>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; {% endfor %}&nbsp; {% endif %}{% endwith %}

缥缈止盈

我找到了该怎么做,问题是我不得不提到我想在神社中使用类别{% with messages = get_flashed_messages( with_categories=true ) %}{% with messages = get_flashed_messages(with_categories=true) %}&nbsp;&nbsp; {% if messages %}&nbsp;&nbsp; &nbsp; &nbsp;{% for category, msg in messages %}&nbsp; &nbsp; &nbsp; &nbsp; <div class="alert {{category}}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h4>{{msg}}</h4>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp;{% endfor %}&nbsp; {% endif %}{% endwith %}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python