Jisson
Jisson

Reputation: 3725

Display dictionary values in template

Hi I have very basic question. I have view like below:

def view1:
    dict = ('one':'itemone','two':'itemtwo','three','itemthree')
    return render_to_response('test.html',dict)

test.html

<body>
{% for key,value in dict.items %}{{ value }}{% endfor %}
</body> 

it doesn't work. Can anyone suggest the correct method to iterate dictionary values in a template. Thanks in advance. Once again am sorry for my basic question.

Upvotes: 5

Views: 16453

Answers (2)

Lou Franco
Lou Franco

Reputation: 89172

I think you want

def view1:
   d = {'one':'itemone', 'two':'itemtwo', 'three':'itemthree'}
   return render_to_response('test.html', {'d':d})

and

<body>
 {% for key,value in d.items %}{{ value }}{% endfor %}
</body> 

Upvotes: 21

Brian from QuantRocket
Brian from QuantRocket

Reputation: 5468

Your dictionary syntax is off. Try this:

my_dict = {'one':'itemone','two':'itemtwo','three':'itemthree'}

(I'd call it something other than dict so you're not overriding python's dict type.)

Upvotes: 3

Related Questions