码迷,mamicode.com
首页 > 其他好文 > 详细

DJANGO之自定义模板过滤器

时间:2016-03-29 10:14:22      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

我查找了DJANGO模板的过滤器,好像指定字符串包含指定关-键字符的过滤器没有呢,

没有硬着头-皮,按网上其它人的作法,写了一个,成功了。。。:)

参考URL:

http://liuzhijun.iteye.com/blog/1884630

http://www.xxx.com/html/2013/pythonweb_1017/588.html

templatetags目录下的deploy_filter.py文件如下:

#!/usr/bin/env python
# coding:utf-8

from django import template

register = template.Library()


@register.filter(name=‘str_contain‘)
def str_contain_filter(value, arg=None):

    ‘‘‘查看指定字符串是否包含关-键字‘‘‘

    print arg, value, ‘#############‘
    if arg in str(value):
        return True
    else:
        return False

# register.filter(‘str_contain‘, str_contain_filter)

  

在模板网页里载入方法如下:

{% load deploy_filter  %}

  

使用如下:

{%  if item.app_name|str_contain:"SQL" %}
       have sql hahaha
{% else %}
       have not sql hahaha
{%  endif%}

  

DJANGO之自定义模板过滤器

标签:

原文地址:http://www.cnblogs.com/aguncn/p/5331660.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!