gpt4 book ai didi

python - 无法在 Google App Engine HTML 模板中输出字典键和值

转载 作者:行者123 更新时间:2023-11-28 16:52:07 25 4
gpt4 key购买 nike

我似乎无法让 Python dict 在我的 HTML 模板中输出它的值。以下代码只是打印一个空的 select 元素。

ma​​in.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>

将其更改为下面的代码确实会输出 key :

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 做 {% for key, value in data.items %} 0.96 .
试试这个:

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

关于python - 无法在 Google App Engine HTML 模板中输出字典键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5809647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com