如果这是一个愚蠢的问题,我很抱歉,但我一直在考虑这个问题,并通过Jinja文档查看,但目前为止无济于事。简短背景:我刚刚完成了Udacity的CS101和CS253,并且即将帮助朋友为瑜伽工作室建立预订系统。这一切都在谷歌的AppEngine,就像在列表上迭代时出现KeyError
我希望有类似这样提供瑜伽课程的列表:
Monday
Dynamic Yoga with Mary 6pm
Soft yoga with Susie 8pm
Wednesday
Hatha yoga with Bob 5pm
Hot yoga with Alice 7pm
所以我想获取类的列表,然后看看是否有一个瑜伽星期一上课。如果有的话,我会在星期一和星期一的所有课程中添加'星期一',以此类推。就像这样:
day_output1 = ['Monday',['Dynamic Yoga with Mary 6pm'],['Soft yoga with Su..']]
day_output2 = ['Wednesday',['Hatha yoga with Bob 5pm'],['Hot yoga with Al...']]
,然后将这些增加了整整一个星期的列表,然后将其发送到模板:
weekly_classes = [day_output1, day_output2]
现在,我得到一个KeyError异常,这意味着它找不到密钥,但我不明白为什么?
File "/Users/username/YogaSchemat/yogaschema/main.py", line 113, in get
day = d[n]
KeyError: 1
使用此代码...在此先感谢你们!
d = {
"1": 'Monday',
"2": 'Tuesday',
"3": 'Wednesday',
"4": 'Thursday',
"5": 'Friday',
"6": 'Saturday',
"7": 'Sunday'
}
def get_classes():
yoga_classes = Schema.all() #appengine DB request
if yoga_classes:
weekly_classes = [] #list that will be sent to template
for n in range(1,8):
for e in yoga_classes:
if e.weekday == n:
day = d[n] #getting weekday from d
class_details = [] #
class_details.append(e)
day_output = [day,class_details]
weekly_classes.append(day_output)
self.response.out.write(weekly_classes)
是你的钥匙1还是“1” – 2013-02-26 21:06:37