Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 505
Jisson Avatar asked Oct 20 '11 15:10

Jisson


2 Answers

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> 
like image 87
Lou Franco Avatar answered Oct 03 '22 18:10

Lou Franco


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.)

like image 26
Brian from QuantRocket Avatar answered Oct 03 '22 19:10

Brian from QuantRocket