从 Flask 的选项中选择项目时如何显示 Flash 消息

这是一个 html 或 Jinja 表单


<form role="search" class="navbar-form nav-item" action="filter" method="GET">

<div class="form-row align-items-center">

    <div class="col-auto my-1">

      <select class="custom-select mr-sm-6" name="myselect" id="inlineFormCustomSelect">

        <option value="new" selected> New</option>

        <option value="old">Old</option>

        <option value="randow">Random</option>

        <option value="more">More Videos</option>

        <option value="less">Less Videos</option>

      </select>

    </div>


        <div class="col-auto my-1">

      <select class="custom-select mr-sm-6" id="inlineFormCustomSelect">

        <option value="all course">All Courses</option>

        <option value="free">Free Courses</option>

        <option value="all price">Price - All</option>

        <option value="lowest">Price - Lowest</option>

        <option value="highest">Price - Highest</option>


      </select>

    </div>

</div>

</form>


now how can i create a flask code/view to flash message when any of the options is selected? 如果我能正确理解这个逻辑,我同样可以使用这个逻辑来创建一个过滤系统来对数据库进行排序。谢谢。


慕哥6287543
浏览 120回答 2
2回答

泛舟湖上清波郎朗

如果有任何消息从 flask 应用程序中闪现,那么可以使用 Jinja 轻松地将其显示在 HTML 上。只需将以下代码粘贴到您希望显示即时消息的位置;最好在身体开始之后。{% for message in get_flashed_messages() %}&nbsp; &nbsp; <div class="alert alert-info">&nbsp; &nbsp; &nbsp; &nbsp;{{ message }}&nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp;{% endfor %}如果有 flash 消息(其中一个),上面的代码运行良好,如果没有,则不会出错。笔记类=“警报警报信息”是 bootstrap 的 css 格式化。

catspeake

在显示它们之前检查 flash 消息是否确实存在很重要。如果存在消息,则可以显示它们;如果没有,则不会显示任何内容。在烧瓶中(无论你想显示闪烁的消息)你可以这样做:{% with messages = get_flashed_messages() %}&nbsp; &nbsp; {% if messages %}&nbsp; &nbsp; &nbsp; &nbsp; {% for message in messages %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="alert alert-info" role = "alert">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ message }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; {% endfor%}&nbsp; &nbsp; {% endif %}{% endwith %}可以从flask-bootstrap 框架中的示例根据您的喜好修改警报。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python