欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Django过滤器

程序员文章站 2022-03-08 08:02:37
# 测试:这些数据类型是否都能传递给前端页面def index(request): n = 123 f = 11.11 s = '团结互助' b = True l = ['小王', '小赵', '小张', ['test1', 1, True, '123'], {'user': 'abc'}] t = (11, 22, 33) d = {'username': 'jason', 'age': 18, 'info': '这个人很有趣'} se =...
# 测试:这些数据类型是否都能传递给前端页面
def index(request):
    n = 123
    f = 11.11
    s = '团结互助'
    b = True
    l = ['小王', '小赵', '小张', ['test1', 1, True, '123'], {'user': 'abc'}]
    t = (11, 22, 33)
    d = {'username': 'jason', 'age': 18, 'info': '这个人很有趣'}
    se = {'张三', '李四', '王五'}

    def func():
        print('触发了func函数')
        return '你好'

    class MyClass(object):
        def get_self(self):
            return 'MyClass-->get_self'

        @staticmethod
        def get_func():
            return 'MyClass-->get_func(staticmethod)'

        @classmethod
        def get_class(cls):
            return 'MyClass-->get_class'

    obj = MyClass()
    file_size = 12345678987654321
    import datetime
    current_time = datetime.datetime.now()

    info = '锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦。'
    english = 'my name is egon. i am from China.'
    msg = 'who are you'
    hhh = '<h1>敏敏</h1>'

    from django.utils.safestring import  mark_safe
    res = mark_safe('<h1>Django</h1>')

    return render(request, 'index.html', locals())
<body>

<p>{{n}}</p>
<p>{{f}}</p>
<p>{{s}}</p>
<p>{{b}}</p>
<p>{{l}}</p>
<p>{{d}}</p>
<p>{{t}}</p>
<p>{{se}}</p>
<p>{{ func }}</p>

<p>{{ MyClass }}</p>
<p>{{ obj }}</p>
<p>{{ obj.get_class }}</p>
<p>{{ obj.get_func }}</p>
<p>{{ obj.get_self }}</p>

<p>{{ l.3.3 }}</p>
<p>{{ l.4.user }}</p>

<p>--过滤器--</p>
<p>统计长度: {{ s | length }}</p>
<p>默认值: {{ b | default:'前面没有值就用 defalt 后面的' }}</p>
<p>文件大小: {{ file_size | filesizeformat }}</p>
<p>日期格式化:{{ current_time | date:'Y-m-d H:i:s' }}</p>
<p>切片操作(支持步长): {{ l | slice:'0:4:2' }}</p>
<p>切取字符(包含三个点)(文章摘要处理): {{ info | truncatechars:6 }}</p>
<p>切取英文(不包含三个点)(中英文都按照空格切分,展示6个单词): {{ english | truncatewords:6 }}</p>
<p>移除特定的字符(可以移除空格): {{ msg | cut:' ' }}</p>
<p>拼接操作: {{ l | join:'$' }}</p>
<p>拼接操作(加法): {{ n | add:10 }}</p>  # add冒号后面不能是空格
<p>拼接操作(字符串): {{ s | add:msg }}</p>
<p>转义(safe告诉浏览器按照html格式显示):{{ hhh | safe }}</p>
<p>转义:{{ res }}</p>

</body>

本文地址:https://blog.csdn.net/weixin_45982321/article/details/110916687

相关标签: Python django