如果这是一个愚蠢的问题,我很抱歉,但我一直在思考这个问题并查看了 Jinja 文档,但到目前为止无济于事。简短背景:我刚刚完成了 Udacity 的 CS101 和 CS253,正准备帮助朋友建立一个瑜伽工作室的预订系统。这一切都在 Google Appengine 上,就像
我想要一个类似这样的可用瑜伽课程列表:
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)