ketanddadhe
ketanddadhe

Reputation: 45

How to pass list containing dictionary in html template and access its values

In django i have this list

my_list=[{'id':1,'username':'sometext'},{'id':2,'username':'someothertext'}]

i am passing this list into the template

   return render(request,'template.html',{'mylist':my_list})

This is my template.html

               {% for i in mylist.items %}
                   <tr>
                                        
                      <td>{{ mylist.id }}</td>
                      <td>{{ mylist.username }}</td>
                                 
                   </tr>
               {% endfor %}

But by doing this i didn't get the value of id and and username.

Upvotes: 0

Views: 1382

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477749

mylist is a list, not a dictionary, so you should enumerate with:

{#     no .items ↓    #}
{% for item in mylist %}
  <tr>
    {# use item ↓ #}
    <td>{{ item.id }}</td>
    <td>{{ item.username }}</td>
  </tr>
{% endfor %}

Upvotes: 1

Related Questions