0

I having a dictionary

a = {'name': u'45445454',
     'tracks': [{'A_TITLE': u'abb',
             'IS': u'144',
             'PN': u'3',
             'T_TITLE': u'123'},
            {'A_TITLE': u'abb',
             'IS': u'45454454',
             'PN': u'3',
             'T_TITLE': u'225'},
            {'A_TITLE': u'ggg',
             'IS': u'232',
             'PN': u'000',
             'T_TITLE': u'555'}]}

I want to redner this dict to my html page.

my html code is not working.

<table>
<tr>
{% for e in tracks %}
<td> e['IS']</td>
<td> e['PN']</td>
....
.,..
 {% endfor %}
<tr>
</table>

The above code throws an error.

I have changed this to

<table>

{% for e in tracks %}
<tr> <td> Title </td> <td> {{ e.A_TITLE }}  -  PN {{ e.PN }}</td>
<tr> <td> tc </td><td> {{e.T_TITLE }} - ISRC {{e.IS }} </td></tr>
   {% endfor %}

</table>

Now it is working good , And you see the dict the A_Title and PN key are dependent.

I want this to be render in html page like.

<tr> <td> abb - 3 </td>
     <td> 123 - 144</td>
     <td> 225 - 45454454</td>
</tr>
<tr> <td> ggg - 000</td>
     <td> 555 - 232</td>

</tr>

or else in this format

<tr> <td> abb - 3 </td></tr>
<tr>         <td> 123 - 144</td>
         <td> 225 - 45454454</td>
    </tr>
    <tr> <td> ggg - 000</td></tr>
<tr>         <td> 555 - 232</td>

    </tr>
5
  • 1
    use {{ e.IS }} instead e['IS'] Commented Apr 17, 2015 at 9:33
  • May first rummage in docs.djangoproject.com/en/1.8/ref/templates/language/#variables before you move on with templates to understand the basic ideas. Commented Apr 17, 2015 at 9:34
  • @GrijeshChauhan Kindly see my edit and give me the solution Commented Apr 17, 2015 at 9:53
  • @AKyhoo move your <tr> tags inside loop, Btw, your closing <tr> tag is incorrect Commented Apr 17, 2015 at 9:57
  • Yes, Please check now Commented Apr 17, 2015 at 10:03

3 Answers 3

2

You have to use e.IS in Django templates

Sign up to request clarification or add additional context in comments.

1 Comment

actually {{ }} to render it as @che
2

Django templates do not use Python, but are a special language, where constructs like e['IS'] do not work. For this particular instance, use simply {{e.IS}} to substitute value of the field.

See https://docs.djangoproject.com/en/1.8/ref/templates/language/ for complete docs of the language.

1 Comment

for OP: additionally if you have list or tuple in context then you would to like {{ L.0 }}, {{ L.1 }} but not {{ L[0] }}, {{ L[1] }} (here Lis a list)
0

Use following inside loop

<td>{{ e.IS }}</td>
<td>{{ e.PN }}</td>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.