1、在settings中的INSTALLED_APPS配置當(dāng)前app,不然django無(wú)法找到自定義的simple_tag.
2、在app中創(chuàng)建templatetags模塊(模塊名只能是templatetags)
3、創(chuàng)建任意 .py 文件,如:myfilter.py
from django import template
register = template.Library() #register的名字是固定的,不可改變
@register.filter
def filter1(value):
print(type(eval(value)))
print(eval(value))
return eval(value)
html中的用法
{# 加載自定過(guò)濾器 #}
{% load myfilter %}
for語(yǔ)句
{% for template_content1 in template_obj.TemplateContent|filter1 %}
{% endfor %}
普通的值
{{ template_obj.TemplateContent|filter1 }}