Thomas Buckley Thomas Buckley - 15 days ago 4
Python Question

Cannot output Dict key & value in Google App Engine html template

I cant seem to get a python dict to output its value in my html template.
The following code simply prints an empty select element.

main.py

feeds = {'feed1' : 'http://test1.xml',
'feed2' : 'http://test2.xml'}

template_values = {'movieFeeds': feeds}

self.response.out.write(template.render(temp, template_values))


index.html

<select>
{% for (key,value) in movieFeeds.items %}
<option value="{{value}}">{{key}}</option>
{% endfor %}
</select>


Changing it to the below does output the key:

for key in movieFeeds


Any ideas where I'm going wrong?

Edit 1



New code

{% for (key,value) in movieFeeds.items() %}
<option value="{{key}}">{{key}}</option>
{% endfor %}


And error:

Traceback (most recent call last):
File "/opt/google_appengine/google/appengine/ext/webapp/__init__.py", line 634, in __call__
handler.get(*groups)
File "/home/thomas/workspace/movieMashup/src/main.py", line 71, in get
self.response.out.write(template.render(temp, template_values))
File "/opt/google_appengine/google/appengine/ext/webapp/template.py", line 72, in render
t = load(template_path, debug)
File "/opt/google_appengine/google/appengine/ext/webapp/template.py", line 100, in load
template = django.template.loader.get_template(file_name)
File "/opt/google_appengine/lib/django_0_96/django/template/loader.py", line 80, in get_template
template = get_template_from_string(source, origin, template_name)
File "/opt/google_appengine/lib/django_0_96/django/template/loader.py", line 88, in get_template_from_string
return Template(source, origin, name)
File "/opt/google_appengine/lib/django_0_96/django/template/__init__.py", line 158, in __init__
self.nodelist = compile_string(template_string, origin)
File "/opt/google_appengine/lib/django_0_96/django/template/__init__.py", line 174, in compile_string
return parser.parse()
File "/opt/google_appengine/lib/django_0_96/django/template/__init__.py", line 273, in parse
compiled_result = compile_func(self, token)
File "/opt/google_appengine/lib/django_0_96/django/template/loader_tags.py", line 154, in do_extends
nodelist = parser.parse()
File "/opt/google_appengine/lib/django_0_96/django/template/__init__.py", line 273, in parse
compiled_result = compile_func(self, token)
File "/opt/google_appengine/lib/django_0_96/django/template/loader_tags.py", line 132, in do_block
nodelist = parser.parse(('endblock', 'endblock %s' % block_name))
File "/opt/google_appengine/lib/django_0_96/django/template/__init__.py", line 273, in parse
compiled_result = compile_func(self, token)
File "/opt/google_appengine/lib/django_0_96/django/template/defaulttags.py", line 542, in do_for
sequence = parser.compile_filter(bits[3])
File "/opt/google_appengine/lib/django_0_96/django/template/__init__.py", line 338, in compile_filter
return FilterExpression(token, self)
File "/opt/google_appengine/lib/django_0_96/django/template/__init__.py", line 558, in __init__
raise TemplateSyntaxError, "Could not parse the remainder: %s" % token[upto:]
TemplateSyntaxError: Could not parse the remainder: ()


Thanks

Answer

You can't do {% for key, value in data.items %} with Django 0.96.
Try with this instead:

{% for item in movieFeeds.items %}
     <option value="{{ item.0 }}">{{ item.1 }}</option>   
{% endfor %}
Comments