【问题标题】:Python Return a list in BottlePython在Bottle中返回一个列表
【发布时间】:2016-01-14 04:48:53
【问题描述】:

我有一个来自 mysql 查询的列表,我试图在我的瓶子网站中返回该列表。这可能吗?这是我所拥有的:

def create_new_location():

    kitchen_locations = select_location()

    return template('''
        % for kitchen_location in {{kitchen_locations}}:
            <a href="/{{kitchen_location}}/">{{kitchen_location}} Kitchen</a>
            <br/>
        % end''',kitchen_locations=kitchen_locations)

这是我得到的错误。

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/bottle.py", line 862, in _handle
return route.call(**args)
  File "/usr/local/lib/python2.7/site-packages/bottle.py", line 1732, in wrapper
rv = callback(*a, **ka)
  File "index.py", line 32, in create_new_location
</form>''',kitchen_locations=kitchen_locations)
  File "/usr/local/lib/python2.7/site-packages/bottle.py", line 3609, in template
return TEMPLATES[tplid].render(kwargs)
  File "/usr/local/lib/python2.7/site-packages/bottle.py", line 3399, in render
self.execute(stdout, env)
  File "/usr/local/lib/python2.7/site-packages/bottle.py", line 3386, in execute
eval(self.co, env)
  File "<string>", line 6, in <module>
TypeError: unhashable type: 'set'

【问题讨论】:

  • 不,那是不可能的
  • 您遇到了什么错误?
  • 我本质上是想显示一个 mysql 查询并在每个项目上放置一个链接。

标签: mysql python-2.7 bottle


【解决方案1】:

知道了(花了我一段时间...)

 % for kitchen_location in {{kitchen_locations}}:

应该是

 % for kitchen_location in kitchen_locations:

在开头使用 % 时,您不需要 {{}}。

这个错误:

TypeError: unhashable type: 'set'

正在尝试使用一组文字 {{kitchen_locations}} ==>

kitchen_locations 在另一个集合中的集合中。因为 set 不是可散列的,所以你得到了错误

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-15
  • 2021-03-01
  • 2016-06-23
相关资源
最近更新 更多