I am new to django. I am not able to render a template using two or multi level dictionary.Below are my view and template code.
code from view.py
myList = {
'ParentABC': {
'ABC' : '#'
}
}
return render_to_response('index.html', myList)
I have tried with two different templates but no luck: Template1-
<ul class="collapsible collapsible-accordion">
{% for eachCategory in myList %}
<li class="bold"><a class="collapsible-header waves-effect waves-teal">{{ eachCategory }}</a>
<div class="collapsible-body" style="">
<ul>
{% for subCat in myList.eachCategory %}
<li><a href="#">{{ subCat }}</a></li>
{% endfor %}
</ul>
</div>
</li>
{% endfor %}
</ul>
template 2-
<ul class="collapsible collapsible-accordion">
{% for category,value in myList.items %}
<li class="bold"><a class="collapsible-header waves-effect waves-teal">{{ category }}</a>
<div class="collapsible-body" style="">
<ul>
{% for subcategory,value1 in value.items %}
<li><a href="#">{{ subcategory }}</a></li>
{% endfor %}
</ul>
</div>
</li>
{% endfor %}
</ul>
after rendering i am always getting below html:
<ul class="collapsible collapsible-accordion">
</ul>
Please help me for the same.
Use following code sample
myList = {
'myList' : {
'ParentABC1': {
'ABC' : '#',
'DEF' : '#'
},
'ParentABC2': {
'ABC' : '#',
'DEF' : '#'
},
}
}
<ul class="collapsible collapsible-accordion">
{% for eachCategory, value in myList.items %}
<li class="bold"><a class="collapsible-header waves-effect waves-teal">{{ eachCategory }}</a>
<div class="collapsible-body" style="">
<ul>
{% for subCat in value %}
<li><a href="#">{{ subCat }}</a></li>
{% endfor %}
</ul>
</div>
</li>
{% endfor %}
</ul>
Use {% for key, value in object.iteritems %}
if you want to iterate on an object with keys and values, or just {% for value in object.items %}
Something like
<ul class="collapsible collapsible-accordion">
{% for eachCategory, catValue in myList.iteritems %}
<li class="bold"><a class="collapsible-header waves-effect waves-teal">{{ eachCategory }}</a>
<div class="collapsible-body" style="">
<ul>
{% for subCat in catValue.items %}
<li><a href="#">{{ subCat }}</a></li>
{% endfor %}
</ul>
</div>
</li>
{% endfor %}
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With