python tornado 中使用 flash消息闪现

1.html 中引入文件

 

{% block head %}
<link href="/static/common/sweetalert/sweetalert.css" rel="stylesheet">
<script src="/static/common/sweetalert/sweetalert.min.js"></script>
{% end %}

 

2.html 页面中设置

  html页面代码与flask相同

#普通闪现
{% for message in get_flashed_messages() %}
    <p style="background-color: #bce8f1">{{ message }}</p>
{% end %}
#分类闪现
{% for  category, message in get_flashed_messages(with_categories=True) %}
    {% if category == 'error' %}
        <p style="{{ message }}</p>
    {% elif category == 'success'%}
        <p style="background-color: green">{{ message }}</p>
    {% end %}
{% end %}
#过滤闪现
<!--  过滤闪现 -->
       {% for  message in get_flashed_messages(category_filter=["error"]) %}
           <p style="background-color: red">{{ message }}</p>
       {% end %}

       {% for  message in get_flashed_messages(category_filter=["success"]) %}
           <p style="background-color: #bce8f1">{{ message }}</p>
       {% end %}
#弹窗
{% for category, message in get_flashed_messages(with_categories=True) %}
    {% if category == 'error' %}
    < script type = "text/javascript" >
        swal({
            'title': '错误',
            'text': '{{ message }}',
            'type': 'error',
            'showCancelButton': false,
            'showConfirmButton': false,
            'timer': 2000
        });
    < / script >
    {% elif category == 'success' %}
        < script type = "text/javascript" >
            swal({
                'title': '正确',
                'text': '{{ message }}',
                'type': 'success',
                'showCancelButton': false,
                'showConfirmButton': false,
                'timer': 2000,
            })
        < / script >
    {% end %}
{% end %}

3. 导入flash文件

  flash.py

#coding=utf-8

def flash(self, message, category='message'):
    """先调用flash"""
    flashes = self.session.get('_flashes', [])
    flashes.append((category, message))  #[('error', '保存失败'),('ok', '分类保存了')]
    self.session.set('_flashes', flashes)


def get_flashed_messages(self, with_categories=False, category_filter=[]):
    """后调用get_flashed_messages
     {% for category, message in get_flashed_messages(with_categories=True) %}
     {% if category == 'error' %}

    """
    flashes = self.flashes
    if flashes is None:
        self.flashes = flashes = self.session.get('_flashes', [])
        del self.session['_flashes']
    if category_filter:
        flashes = list(filter(lambda f: f[0] in category_filter, flashes))
    if not with_categories:
        return [x[1] for x in flashes]
    return flashes #[('error', '保存失败')]

 

4. tornado 传递函数

from libs.flash.flash_lib import get_flashed_messages
from libs.permission.permission_auth.permission_interface_libs import menu_permission

settings = dict(
        template_path = 'templates',
        static_path = 'static',
        debug = True,
        cookie_secret = 'aaaa',
        login_url = '/auth/user_login',
        xsrf_cookies = True,
        ui_methods= {
            "menu_permission": menu_permission,
            "get_flashed_messages": get_flashed_messages # flash 消息闪现函数
        },
        # pycket的配置信息
        pycket = {
             'engine': 'redis',  # 设置存储器类型
             'storage': {
                 'host': 'localhost',
                 'port': 6379,
                 'db_sessions': 5,
                 'db_notifications': 11,
                 'max_connections': 2 ** 31,
             },
             'cookies': {
                 'expires_days': 30,  # 设置过期时间
                 #'max_age': 5000,
             },
         },
)

 5.代码中设置消息内容

  • 注意先导入 flash 文件
  • 示例:  

def
del_role_lib(self, roleid): """03删除角色""" role= Role.by_id(roleid) if role is None: flash(self, "角色删除失败", "error")
    
return self.db.delete(role) self.db.commit() flash(self, "角色删除成功", "success")

 

 

转载于:https://www.cnblogs.com/zlsgh/p/8994900.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值