3

I have a dictionary:

{'m': 'm', 'c': None, 's': None, 'n': None, 'a': 'a', 'x': None, 'b': None, 'l': None, 'u': None, 'o': 'o', 'q': None, 'i': None, 'f': None, 'z': None, 'e': None, 't': 't', 'h': None, 'y': None, 'v': None, 'p': None, 'k': None, 'g': None}

I'd like to hand the key value pairs that have the value None differently than the key value pairs that have the letters.

Heres what I have so far for the template

<table align="center" cellpadding="4px">
            <tr>
           {% for key, value in index_display.items %}
               {% if index_display[key] = None %}
                    <td><a href="">{{ key }}</a></td>
                    <td id="partners_nav_bracket">|</td>
                {% else %}
                    <td><a href="{{ value }}">{{ key }}</a></td>
                    <td id="partners_nav_bracket">|</td>
                {% endif %}
            {% endfor %}
        </tr>
   </table>

This however throw an error:

Could not parse the remainder: '()' from 'index_display.key()'
halfer
  • 19,824
  • 17
  • 99
  • 186
questnofinterest
  • 309
  • 1
  • 3
  • 12
  • 1
    Possible duplicate of [Accessing dictionary by key in Django template](https://stackoverflow.com/questions/19745091/accessing-dictionary-by-key-in-django-template) – souldeux Nov 01 '17 at 17:21
  • 1
    @souldeux That question does not provide a way for accessing the key and value of the dictionary thats being iterated upon. – questnofinterest Nov 01 '17 at 17:24
  • You already have the key *and* value when you loop through items. There is no need to try `index_display[key]` (which is lucky, because the Django template language doesn't allow dictionary lookups like this). – Alasdair Nov 01 '17 at 17:52
  • Your error message doesn't seem to match the template in your question. You don't use `index_display.key()` anywhere. – Alasdair Nov 01 '17 at 17:53
  • @Alasdair Sorry, I updated the for loop in the question and forgot to update that part as well. – questnofinterest Nov 01 '17 at 18:04

1 Answers1

4
<table align="center" cellpadding="4px">
    <tr>
    {% for key, value in index_display.items %}
        {% if value == None %}
            <td><a href="">{{ key }}</a></td>
            <td id="partners_nav_bracket">|</td>
        {% else %}
            <td><a href="{{ value }}">{{ key }}</a></td>
            <td id="partners_nav_bracket">|</td>
        {% endif %}
    {% endfor %}
    </tr>
</table>
knbk
  • 52,111
  • 9
  • 124
  • 122
N. L. Long
  • 180
  • 4