provola provola - 5 months ago 31
Python Question

Error in python function pymongo

This is a function that returns an error at the line with


This is the error:

in get_posts
post['date'] = post['date'].strftime("%A, %B %d %Y at %I:%M%p")
KeyError: 'date'

What does it mean?

This is the function:

def get_posts(self, num_posts):

cursor = self.posts.find({},{}).limit(num_posts) # Using an empty itable for a placeholder so blog compiles before you make your changes
# XXX HW 3.2 Work here to get the posts
l = []

for post in cursor:
print post
post['date'] = post['date'].strftime("%A, %B %d %Y at %I:%M%p")
if 'tags' not in post:
post['tags'] = [] # fill it in if its not there already
if 'comments' not in post:
post['comments'] = []

l.append({'title':post['title'], 'body':post['body'], 'post_date':post['date'],

return l


KeyError: 'date' means that post doesn't have the attribute date.