我似乎无法让Python dict
在我的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: ()
答案 0 :(得分:2)
您无法使用Django 0.96执行{% for key, value in data.items %}
请尝试使用此功能:
{% for item in movieFeeds.items %}
<option value="{{ item.0 }}">{{ item.1 }}</option>
{% endfor %}
答案 1 :(得分:1)
如@systempuntoout所示,Django 0.96不支持此功能。您可以改为tell App Engine to use Django 1.2:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.2')