Skip to content Skip to sidebar Skip to footer

Django Nested Template Tags

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_

Solution 1:

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

Solution 2:

# 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 ...

Post a Comment for "Django Nested Template Tags"