Django嵌套模板标签 [英] Django Nested Template Tags

查看:57
本文介绍了Django嵌套模板标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个访问模板功能的自定义模板标签.但是,我还需要将自定义模板标记置于for循环中,这需要嵌套的模板标记:

I have a custom template tag that accesses a models function. However, I also need the custom template tag to be in a for loop, which requires nested template tags:

{% 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中的项目.反正我能做到吗?

It gives me a TemplateSyntaxError- 'for' statements should use the format 'for x in y': for item in {% get_list_items user.username. Is there anyway I can do this?

自定义标签:

register = template.Library()

@register.simple_tag
def get_list_items(event_ins, authenticated_user):
    return event_ins.get_list_items(authenticated_user)

推荐答案

您不能以这种方式嵌套标签-但您可以将标签的输出分配给一个变量,然后可以对其进行循环:

You can't nest tags in this way - but you can assign the output of the tag to a variable that you can then loop over:

{% 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 %}

这篇关于Django嵌套模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆