Search code examples
djangodjango-template-filters

How to display N number of Backward relationship in Django templates?


{% for category in categories %}
    {% for product in categories.product_set.all %}
        <h1> {{ product.name }} </h1>
    {% endfor %}
{% endfor %}

I want to show 10 products instead of all in template


Solution

  • There is a slice filter that you can use in templates:

    {% for category in categories %}
        {% for product in categories.product_set.all|slice:":10" %}
            <h1> {{ product.name }} </h1>
        {% endfor %}
    {% endfor %}