Django templates not showing

I am new to django. I cannot display a template using a 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 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> 

pattern 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 always get below html:

 <ul class="collapsible collapsible-accordion"> </ul> 

Please help me for the same.

+5
source share
2 answers

Use the following code example

  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> 
+1
source

Use {% for key, value in object.iteritems %} if you want to iterate the object using keys and values ​​or just {% for value in object.items %}

Sort of

 <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> 
+1
source

Source: https://habr.com/ru/post/1238588/


All Articles