Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django templates are not rendering

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.

like image 880
Gaurav Pant Avatar asked Dec 18 '15 02:12

Gaurav Pant


2 Answers

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>
like image 186
r.bhardwaj Avatar answered Oct 19 '22 19:10

r.bhardwaj


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>
like image 26
jmbarbier Avatar answered Oct 19 '22 19:10

jmbarbier