zoukankan      html  css  js  c++  java
  • 时间过滤器的设计(实现输出几分钟前,几小时前,几天前)

    功能如下

    如下图的右下角3分钟前。如果是1分钟内设定为刚刚,等等。

    过滤器

    在templatetags里面新建一个date_filter的时间转化功能。

    大概的功能就是在template里面添加一个过滤器,并且加在传过来的参数旁边。

     

     注意一定要加_init_

     不然在模板里面是不能加载进去。

    过滤器的后端设计

    直接模板显示的数据

    在templatetags里面设计一个date_filter然后load在对应的html页面上面,然后使用。

    from django import template
    from datetime import datetime
    import pytz
    
    # 注册过滤器
    register = template.Library()
    
    # data 是从数据库那边传过来的
    @register.filter()
    def date_filter(data):
        # 判断data是否datetime的实例
        if isinstance(data, datetime):
            # 获取最新时间
            now = datetime.now()
            now = now.replace(tzinfo=pytz.timezone('UTC'))
            # 确定settings里面的设置
            print('最新时间{}'.format(now))   # 大陆时间
            print('数据库的时间{}'.format(data))
            timestamp = (now-data).total_seconds()
            if timestamp < 60:
                return '刚刚'
            elif timestamp>=60 and timestamp<60 *60:
                minu = int(timestamp//60)
                return '{}分钟前'.format(minu)
            elif timestamp >=60*60 and timestamp <60*60*24:
                hour = int(timestamp//(60*60))
                return '{}小时前'.format(hour)
            elif timestamp >=60*60*24 and timestamp <60*60*24*30:
                day = int(timestamp//(60*60*24))
                return '{}天前'.format(day)
            elif timestamp >=60*60*24*30 and timestamp <60*60*24*365:
                mon = int(timestamp//(60*60*24*30))
                return '{}月前'.format(mon)
            elif timestamp >=60*60*24*365 and timestamp <60*60*24*365*2:
                year = int(timestamp//(60*60*24*365))
                return '{}年前'.format(year)
            elif timestamp>60*60*24*365*2:
                return '莫得了'
            else:
                return data.strftime('%Y-%m-%d %H:%M')
        else:
            return data

    JS的Ajax响应生成

    这个由于是从异步响应生成的数据。所以并不能在JS页面上面套进去,添加一个数据过滤器。所以要从数据源头下手。

    news_info_list = []
            for n in news_info:
                news_info_list.append(
                    # 过滤器
                    n.to_put()
                )
    data = {
    'total_pages': paginator.num_pages,
    'news': news_info_list,
    }

    第一个是从template里面的html下手,这次是从模型数据获取上面下手。然后返回一串json数据。

    #   在News模型里面添加处理函数
    from news.templatetags.date_filter import date_filter
    def to_put(self):
            dict = {
                'id': self.id,
                'title': self.title,
                'digest': self.digest,
                'image_url': self.image_url,
                'tag_name': self.tag.name,
                'author': self.author.username,
                'update_time': date_filter(self.update_time),
            }
            return dict
  • 相关阅读:
    【CLR Via C#】2 程序集
    值类型与引用类型
    .Net Framework简介
    【CLR Via C#】15 枚举类型与位类型
    Dictionary的用法及用途
    枚举
    GitExtensions使用教程
    Bootstrap如何禁止响应式布局
    【StyleCop】StyleCop规则汇总
    优化SQL查询:如何写出高性能SQL语句
  • 原文地址:https://www.cnblogs.com/jackson669/p/12795180.html
Copyright © 2011-2022 走看看