【发布时间】:2018-04-03 14:23:43
【问题描述】:
我目前正在使用自定义templatetags 减少我的模板代码。我本质上是将上下文从视图传递到标签,然后将其渲染到共享模板中。但是,我遇到了一个我无法解决的问题。
我传递给标签的每个视图的上下文都包含一个以'_collection' 结尾的键。本质上,我需要遍历上下文并找到与该子字符串对应的键/值对,然后将其映射到标记上下文。
这是我正在使用的模板标签:
from django import template
register = template.Library()
@register.inclusion_tag('main/collection.html', takes_context=True)
def collection(context):
ctx = {
'is_create': context['is_create']
}
if '_collection' in context:
# not sure how to get this into the context
return ctx
基本上我只是想通过使用该子字符串将该值正确映射到模板标签上下文。
任何帮助将不胜感激。
感谢您的宝贵时间。
【问题讨论】:
标签: django templatetags