您的位置:首页 > 编程语言 > Go语言

django10 使用自定义标签配置说明

2017-08-04 00:00 344 查看
1)、在app目录下建目录templatetags【不可改名】目录,然后在该目录下建一个空的__init__.py


2)、mytags.py
在templatetags下建一个mytags.py,添加:

from django import template
register = template.Library()


添加一个函数,用于html页面调用。例如:

import re , time , datetime,os,sys
from lykops.settings import BASE_DIR

@register.filter
def is_image(value):
[code=python]    value = str(value)
#请使用str,否则页面报错
full_filename = BASE_DIR + '/' + value if re.search('.jpg$' , value) : if os.path.exists(full_filename) and os.path.isfile(full_filename) : return '<img alt="image" src="/' + value + '" />' else : return value else : return value

3)、html页面应用

在html页面上添加{% load mytags %}
调用方式

				{% autoescape off %}
<td>{{ file |is_image  }}</td>
{% endautoescape %}


注意:{% autoescape off %}是为了html对这段代码<td>{{ file |is_image }}</td>不转义





[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: