具有上下文变量参数的Django自定义模板标记
发布时间:2021-02-21 02:30:52 所属栏目:Python 来源:互联网
导读:我有一个自定义模板标签,显示日历.我想根据动态值填充日历上的某些项目. 这是标签: @register.inclusion_tag(website/_calendar.html)def calendar_table(post): post=int(post) imp=IMP.objects.filter(post__pk=post) if imp:
我有一个自定义模板标签,显示日历.我想根据动态值填充日历上的某些项目. 这是标签: @register.inclusion_tag("website/_calendar.html") def calendar_table(post): post=int(post) imp=IMP.objects.filter(post__pk=post) if imp: ...do stuff 在我的模板中,当我传递硬编码值时,它可以正常工作,例如 {% load inclusion_tags %} {% calendar_table "6" %} 但是,当我尝试类似{%calendar_table“{{post.id}}”%}的内容时,会为int()尝试引发一个错误ValueError.我怎么能绕过这个? 解决方法你想要{%calendar_table post.id%};额外的{{和}}是导致你胃灼热的原因.请注意,在自定义标记中,您需要获取传递的字符串(“post.id”)并使用Variable.resolve针对上下文解析它.在Django文档中有更多关于它的信息;特别是,看这里:http://docs.djangoproject.com/en/1.3/howto/custom-template-tags/#passing-template-variables-to-the-tag (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |