zoukankan      html  css  js  c++  java
  • 自制过滤器

    流程:

    1. 在App中创建一个python包 templatetags ;包下面创建一个python文件( my_filter )用来存储过滤器;
    2. 在新建的python文件定义过滤器(函数),函数第一个参数为被过滤的值,最多可传两个参数;后需用进行注册;
      1 #my_filter.py
      2 from django import template
      3 register = template.Library()
      4 
      5 def greet(value,world):
      6     return value + world
      7 
      8 #注册
      9 register.filter('greet',greet)

      # 使用装饰器

      1 from django import template
      2 register = template.Library()
      3 
      4 @register.filter('greet')    #可为过滤器命名
      5 def greet(value,world):
      6     return value + world
    3. 将过滤器所在的APP添加到 ‘settings.INSTALLED_APS’ 中;
      1 #settings.py
      2 '''
      3 INSTALLED_APPS = [
      4     ...
      5     'front'  #添加app
      6 ]
      7 '''
    4. 在模板使用 load 标签加载过滤器所在的python包;
      1 <!--加载过滤器-->
      2 {% load my_filter %}
      3 <!DOCTYPE html>
      4 ...
      5 <body>
      6     {{ value|greet:'欢迎' }}
      7 </body>
      8 </html>
    5. 完成。

    例子:

    1. 在存储过滤器的文件中创建过滤器 tiem_since 函数
       1 from django import template
       2 from datetime import datetime
       3 
       4 register = template.Library()
       5 
       6 @register.filter
       7 def time_since(value):
       8     '''
       9     :param value:输入的时间
      10     :return:输入的时间距离现在的时间间隔(可用于发布动态后显示距离当前时间的时间间隔)
      11     1、如果时间间隔小于1分钟,显示‘刚刚’;
      12     2、如果大于1分钟小于1小时,显示‘xx分钟前’;
      13     3、如果大于1小时小于24小时,显示‘xx小时前’;
      14     4、如果大于24小时小于30天,显示‘xx天前’;
      15     5、否则显示具体时间:xxxx/xx/xx xx/xx;
      16     '''
      17     if not isinstance(value,datetime):
      18         return value
      19     now = datetime.now()
      20     # timedelay.total_seconds() 定义持续时间内的总秒数
      21     timestamp = (now - value).total_seconds()
      22     if timestamp < 60:
      23         return '刚刚'
      24     elif timestamp >= 60 and timestamp < 60*60:
      25         minutes = int(timestamp/60)
      26         return '%s分钟前' % minutes
      27     elif timestamp >= 60*60 and timestamp < 60*60*24:
      28         hours = int(timestamp/60/60)
      29         return '%s小时前'% hours
      30     elif timestamp >= 60*60*24 and timestamp < 60*60*24*30:
      31         days = int(timestamp/60/60/24)
      32         return '%s天前'% days
      33     else:
      34         return value.strftime("%Y/%m%d %H:%M")
    2. 应用的视图函数
      1 from django.shortcuts import render
      2 from datetime import datetime
      3 def index(request):
      4     # content = input('输入你要发表的内容:')
      5     context = {
      6         'value': datetime(year=2019,month=1,day=25,hour=10,minute=20)
      7     }
      8     return render(request,'index.html',context)
    3. HTML模板文件
       1 {% load my_filter %}
       2 <!DOCTYPE html>
       3 <html lang="en">
       4 <head>
       5     <meta charset="UTF-8">
       6     <title>Title</title>
       7     <style>
       8         body{
       9             background: aliceblue;
      10         }
      11     </style>
      12 </head>
      13 <body>
      14     {{ value|time_since }}
      15 </body>
      16 </html>
    4. 在 settings 添加应用及进行URL映射,最终网页输出结果:(以当前随笔时间为准:2019/01/25 21:13)
  • 相关阅读:
    曼昆《经济学原理》(第五版)习题解答 第一章 导 言 经济学十大原理
    Looksery Cup 2015 D. Haar Features 暴力
    Looksery Cup 2015 H. Degenerate Matrix 数学
    Looksery Cup 2015 A. Face Detection 水题
    hdu 5258 数长方形 离散化
    Codeforces Round #306 (Div. 2) E. Brackets in Implications 构造
    Codeforces Round #306 (Div. 2) D. Regular Bridge 构造
    Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力
    Codeforces Round #306 (Div. 2) B. Preparing Olympiad dfs
    Codeforces Round #306 (Div. 2) A. Two Substrings 水题
  • 原文地址:https://www.cnblogs.com/liqiongming/p/10321698.html
Copyright © 2011-2022 走看看