定义一个包含标签inclusion_tag, 调用模板时报错.. 应该是路径 不对吧...我的templates 是放在app 目录下的.<待处理>
# 自定义模板标签. 标签的作用,在模板中 实现逻辑,如if ,for 等 from django.template import Library from datetime import datetime register = Library() # 模板标签 可以 引用 views上下文中的变量 context # 设置 simple_tag中的 takes_context = Ture, context 参数必须写'前面' # def c_time(format_str='%Y-%m-%d'): 直接输入 格式, def c_time(context): # 从上下参数 now = datetime.now() return now.strftime(context['format_str']) # 从上下参数 # 设置 simple_tag中的 takes_context = Ture register.simple_tag(name='c_time', takes_context=True) # 定义一个包含标签inclusion_tag def show_list_as_ul(value): # value 接收来自 context的变量 return {'ls': value} # 将 value 转换成一个ls, 变量传递给 模板 register.inclusion_tag(name='show_list_as_ul', filename='show_list_as_ul.html', takes_context=True) # 接收来自 view 的context变量,用 show_list_as_ul.html模板样式去显示
应该是
register.inclusion_tag(name='show_list_as_ul', filename='show_list_as_ul.html', takes_context=True)
filename 路径不对.