0

I have python dictionary, and I am passing its values to a html page to display them. i am able to access the values of the dictionary by using {{ value.xxx }} where the xxx is the element of the dictionary. the values appear on the screen no problem for name, age & height below. However the skill set component doesn't appear because of the space in the text. How can i set it so that i can access the elements that contain a space, like the skillset value below.

I have tried adding an underscore such as {{ value.skill_set }} but that doesn't seem to work.

Dictionary:

dict = {1: {'name': 'John', 'age': '27', 'height': '160', 'skill set': 'running'},
          2: {'name': 'Marie', 'age': '22', 'height': '170', 'skill set': 'swimming'}}

Html:

<table class="u-full-width" id="table2">
<thead >
    <tr>

    </tr>
  </thead>
 <tbody>
    {% for key,value in dict_items.items %}
    <tr>
        <th scope="row" style="text-align:center">{{ key }}</th>
        <td style="text-align:center">{{ value.name }}</td>
        <td style="text-align:center">{{ value.age }}</td>
        <td style="text-align:center">{{ value.height }}</td>
        <td style="text-align:center">{{ value.skill set }}</td>
    </tr>
    {% endfor %}
</tbody>
</table>
kitchen800
  • 197
  • 1
  • 12
  • 36
  • 1
    Did you try `value['skill set']`? – rdas Nov 18 '21 at 15:03
  • I think it'll throw an error if you try to do `value['skill set']`, but I'm not sure because I haven't touched Django for awhile. –  Nov 18 '21 at 15:06
  • value['skill set'] is throwing an error saying "could not parse the remainder: '['skill set']' from 'value['skill set']' – kitchen800 Nov 18 '21 at 15:09

2 Answers2

1
<table class="u-full-width" id="table2">
<thead >
    <tr>

    </tr>
  </thead>
 <tbody>
    {% for key,value in dict_items.items %}
    <tr>
        <th scope="row" style="text-align:center">{{ key }}</th>

         {% for key_,value_ in value.items %}

        <td style="text-align:center">{{ value_ }}</td>

         {% endfor %}

    </tr>
    {% endfor %}
</tbody>
</table>
Thierno Amadou Sow
  • 2,523
  • 2
  • 5
  • 19
  • this managed to show all the data (including skill set) however I need to be able to select what data is show like my example above. your way shows all the entries but i need to be able to have control of just showing certain enteries that i wish to see. is it possible to use you technique with my original code? – kitchen800 Nov 18 '21 at 15:18
  • @kitchen800 your question was how to show the `skill set with space` and this work i think or i do not understand your question!!! – Thierno Amadou Sow Nov 18 '21 at 15:27
  • true, is there a way to see the flexibility being able to show only certain criteria like the original code? – kitchen800 Nov 18 '21 at 15:33
  • @kitchen800 it is possible but you have to create a custom filter.but i think this is one of the easiest way of doing it.do you know how to create a custom filter in django ? – Thierno Amadou Sow Nov 18 '21 at 15:37
0

Django templates use dot notation and there is no straight forward way to look up a key with a space in it. Your question has been asked before, and some solutions like using custom filters and template tags can be viewed here: Django templates: value of dictionary key with a space in it

TaipanRex
  • 795
  • 5
  • 11