Home > front end >  Looping problem with defauldict in Django templates
Looping problem with defauldict in Django templates

Time:02-25

On my Django , i used defauldict on my views, and to give a structure I put this code in my template , but it s not working nothing happen in my output

What i need to change please ? I want to see in my web page something like this :

.List in the file :
value
value1
value3
...

.List not in the file :
value
value 
value...

.html

<!DOCTYPE html>
<html lang="en">
<head>

    <meta charset="UTF-8">
    <title> Dashboard Result</title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    

CodePudding user response:

Key and value are the key/value pairs of the defaultdict. Change

{% for key, value in results.items %}
         {{ item.key }}
         {{ item.value}}
{% endfor %}

to

{% for key, value in results.items %}
         {{ key }}
         {{ value}}
{% endfor %}

and since value is a list you might want

{% for key, value in results.items %}
         {{ key }}
         {% for elem in value %}
              {{ elem }}
         {% endfor %}
{% endfor %}
  • Related