Python Django嵌套模板标记
我有一个自定义模板标记,可以访问模型函数。但是,我还需要自定义模板标记位于for循环中,这需要嵌套的模板标记:Python Django嵌套模板标记,python,django,Python,Django,我有一个自定义模板标记,可以访问模型函数。但是,我还需要自定义模板标记位于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&
{% 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 %}
#您可以在函数本身中格式化文本或数据,并将其返回模板
{对于提醒列表中的列表%}
{{list.title}}
{{list.id{get_list_items:authenticated_user}
{%endfor%}
register=template.Library()
@register.simple_标记
def获取列表项目(事件、已验证用户):
#您可以在此处格式化文本或数据
返回。。。
您不能以这种方式嵌套标记-但您可以将标记的输出分配给一个变量,然后您可以循环:
{% 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 %}
{%load custom_tags%}
{对于提醒列表中的列表%}
{{list.title}}
{%get\u list\u items list user.username作为list\u items%}
{列表中项目的%u%}
{{item.title}
{%endfor%}
{%endfor%}
您是否已将标签加载到模板顶部?请发布您得到的完整错误。是的,我加载了模板标记,我刚刚发布了完整错误
# 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 ...
{% 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 %}