我似乎无法获得Python字典在HTML模板中输出其值.
以下代码仅打印一个空的select元素.
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>
将其更改为以下代码不会输出密钥:
for key in movieFeeds
有什么想法我要去哪里吗?
编辑:
新代码:
{% for (key,value) in movieFeeds.items() %}
<option value="{{key}}">{{key}}</option>
{% endfor %}
错误:
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: ()
解决方法:
您无法在Django 0.96中执行{%为key.data.items%中的值}.
试试这个代替:
{% for item in movieFeeds.items %}
<option value="{{ item.0 }}">{{ item.1 }}</option>
{% endfor %}