[Answered ]-List data print in another for loop in Django template

1๐Ÿ‘

โœ…

You can make a custom template filter.

Make a custom_filters.py file inside a directory called templatetags inside one of your app directories. For example, if you have an app called myapp, then you should have a directory called myapp/templatetags/ containing the custom_filters.py file.

Then in the custom_filters.py file write the following:

from django import template

register = template.Library()

@register.filter
def get_item(dictionary, key):
    return dictionary.get(key)

Then in the template, you can use the filter like this:

{% load custom_filters %}

{% for package in packages %}
    package name: {{ package }}
    {% with forloop.counter0 as index %}
        {{ package_main_prices_list|get_item:index }}
    {% endwith %}
    date: {{ some_date }}
{% endfor %}

Load the custom filters at the top of the file.

Make sure to restart the server after making changes.

๐Ÿ‘คSunderam Dubey

0๐Ÿ‘

Pass the data in query set. It will be much easier

๐Ÿ‘คSudip Bhandari

Leave a comment