Nirmal Patel
Nirmal Patel

Reputation: 61

issue in printing dictionary value in django

I have two list li_title and a_href as below. I have combined it in to dictionary but it not print any thing in template file.

This is the code in view(value are appended in list with same length):

param = {li_title[i] : a_href[i] for i in range(len(li_title))}
return render(request, 'index.html', param)

This is the code in index.html.

{% for i,j in param.items %}
    {{j}}
{% endfor %}

Upvotes: 3

Views: 48

Answers (1)

lmiguelvargasf
lmiguelvargasf

Reputation: 69725

You are not using the context properly. param is the dictionary that you are passing, and you will be able to use the keys of param to access the values. Try using the following code:

param = {'param': {li_title[i] : a_href[i] for i in range(len(li_title))}}

In this way you will be able to use param to access the associated value.

In your template:

{% for i,j in param.items %}
    {{j}}
{% endfor %}

Upvotes: 3

Related Questions