Home > OS >  How to aceess list item in jinja
How to aceess list item in jinja

Time:11-19

I have 2 list in my Django site.

my views.py:

def index(request):
    totalDBElement = [169, 2166, 5413, 635, 635]
    elementOrder = ['Rules', 'Questions', 'ParentChild', 'ChildList']
    return render(request,'diagnosis/index.html', {'totalDBElement': totalDBElement, 'elementOrder' : elementOrder})  

I wish to get something like this in my template:

Rules: 169

Questions: 2166

ParentChild: 5413

ChildList: 635

my template:

{% for i in len(totalDBElement) %}
<h2> {{ totalDBElement[i] }} </h2>
<h2> {{ elementOrder[i] }} </h2>
{% endfor %}

But it gives errors like:

Could not parse the remainder: '(totalDBElement)' from 'len(totalDBElement)'

Please suggest how can I fix this?

I also wish to print

CodePudding user response:

def index(request):
    totalDBElement = [169, 2166, 5413, 635, 635]
    elementOrder = ['Rules', 'Questions', 'ParentChild', 'ChildList']
    all_in_one = zip(elementOrder,totalDBElement)
    return render(request,'diagnosis/index.html', {'totalDBElement':all_in_one})

now in template

{% for item1, item2 in totalDBElement %}
<h2> {{ item1 }}:{{ item2 }} </h2>
{% endfor %}
  • Related