[Django]-Appending string to dictionary values using jinja template

3๐Ÿ‘

โœ…

You can use it like this with your current data structure.

{% for key, value in names.items %}
    <p class="lead">Name: {{ value.0 }}</p>
    <p class="lead">Id: {{ value.1 }}</p>
    <p class="lead">Date: {{ value.2 }}</p>
{% endfor %}

But it would be more readable if change your data structure into this:

my_dict = {
    'ant': {
        'id': 2,
        'name': 'abc',
        'date' : datetime.now()
    }
}

then your template would look like this:

{% for key, person in my_dict.items %}
    <p class="lead">Id: {{ person.id }} </p>
    <p class="lead">Name: {{ person.name }} </p>
    <p class="lead">Date: {{ person.date }} </p>
{% endfor %}
๐Ÿ‘คKutayAslan

2๐Ÿ‘

In Jinja-2 you can subscript in the variables:

{% for key,vals in names.items %}
    <p class="lead">Name: {{ vals[0] }}</br>
    Id: {{ vals[1] }}</br>
    Date: {{ vals[2] }}</p>
{% endfor %}

Leave a comment