我有一个访问模板功能的自定义模板标签。但是,我还需要将自定义模板标记置于for循环中,这需要嵌套的模板标记:
{% load custom_tags %}
{% for list in remind_lists %}
<h3>{{ list.title }}</h3>
{% for item in {% get_list_items user.username %} %}
<p>{{ item.title }}</p>
{% endfor %}
{% endfor %}
它为我提供了TemplateSyntaxError-'for'语句应使用格式'for for x in y':用于{%get_list_items user.username中的项目。反正我能做到吗?
自定义标签:
register = template.Library()
@register.simple_tag
def get_list_items(event_ins, authenticated_user):
return event_ins.get_list_items(authenticated_user)
答案 0 :(得分:1)
# you can format the text or data in the function itself and return the same to the template
{% for list in remind_lists %}
<h3>{{ list.title }}</h3>
{{ list.id|get_list_items:authenticated_user }}
{% endfor %}
register = template.Library()
@register.simple_tag
def get_list_items(event_ins, authenticated_user):
# you can format the text or data here
return ...
答案 1 :(得分:1)
您不能以这种方式嵌套标签-但您可以将标签的输出分配给一个变量,然后可以对其进行循环:
{% load custom_tags %}
{% for list in remind_lists %}
<h3>{{ list.title }}</h3>
{% get_list_items list user.username as list_items %}
{% for item in list_items %}
<p>{{ item.title }}</p>
{% endfor %}
{% endfor %}