Home > Back-end >  Iterate over a dictionary to display certain values that are not None - in HTML page
Iterate over a dictionary to display certain values that are not None - in HTML page

Time:08-14

I have a database table called streaming_service where I need to get a record from it and put it on a html page. After getting a record from the table and storing it in a variable called stream. I now want to display the contents of the variable stream onto an HTML page. I however dont want to display the values in the variable stream that equal to None.

An example of stream would be:

{'movie_id': 1616014, 'service1': 'https://www.netflix.com/watch/81160697', 'service2': None, 'service3': None, 'service4': None, 'service5': None}

Below is my HTML code using for loops and if conditions. I have said that if the value of a key is None i should continue the loop. But I keep getting thrown with errors.

Any help would be awesome

<h2>Links:</h2>
{% for k, v in stream.items() %}
{% if stream[k] is None %}
{% continue %}
{% else %}
<p ><a href="{{ stream.service1 }}">{{ stream.service1 }}</a></p>
<p ><a href="{{ stream.service1 }}">{{ stream.service2 }}</a></p>
<p ><a href="{{ stream.service1 }}">{{ stream.service3 }}</a></p>
<p ><a href="{{ stream.service1 }}">{{ stream.service4 }}</a></p>
<p ><a href="{{ stream.service1 }}">{{ stream.service5 }}</a></p>
{% endif %}
{% endfor %}
{% endblock content %}         
</div>

I then tried this code but it just duplicated everything including the None values onto the html page.

<h2>Links:</h2>
{% for k, v in stream.items() %}
{% if stream[k] != None %}
<p ><a href="{{ stream.service1 }}">{{ stream.service1 }}</a></p>
<p ><a href="{{ stream.service1 }}">{{ stream.service2 }}</a></p>
<p ><a href="{{ stream.service1 }}">{{ stream.service3 }}</a></p>
<p ><a href="{{ stream.service1 }}">{{ stream.service4 }}</a></p>
<p ><a href="{{ stream.service1 }}">{{ stream.service5 }}</a></p>
{% endif %}
{% endfor %}
{% endblock content %}         
</div>

CodePudding user response:

This code needs many fixes:

1 - It's best to handle the dict logic outside the template so rather than using stream you can first create a stream2 dict with filtered values and using that one

stream = {"movie_id": 1616014, "service1": "https://www.netflix.com/watch/81160697", "service2": None, "service3": None, "service4": None, "service5": None}
stream_filtered = {}
for key, value in stream.items():
    if not value:
        continue
    stream_filtered[key] = value
# check with print(stream2)

2 - Inside the template forloop you should not repeat the paragraph tag what would the purpose of the forloop be then?

{% for k, v in stream.items() %}
  {% if stream[k] is None %}
    {% continue %}
  {% else %}
    <p ><a href="{{ stream.service1 }}">{{ stream[k] }}</a></p>
  {% endif %}
{% endfor %}

This assumes that you want all the p's to have the same class and href (that's most likely not what you want)

3 - Since you are using the dict.items() method and you have key and value, use value rather than referencing so you can rewrite the previous like this:

{% for key, value in stream.items() %}
  {% if value is None %}
    {% continue %}
  {% else %}
    <p ><a href="{{ stream.service1 }}">{{ value }}</a></p>
  {% endif %}
{% endfor %}

4 - If you have done step 1 you don't need the if statement at all in the template but if you want to check None in the template you don't need the else statement since you are using the continue

{% for key, value in stream.items() %}
  {% if value is None %}
    {% continue %}
  {% endif %}
  <p ><a href="{{ stream.service1 }}">{{ value }}</a></p>
{% endfor %}

5 - Since the keys of this dictionary are basically integers you would be better serverd with a List rather than a dcitionary

  • Related