views:

56

answers:

1

I'm using the Tornado framework (Python) on GAE. I'm still kind of new to the whole MVC concept and GAE... and having a dang of a time trying to figure out how to do this.

I have a table (model) Post with the fields user, text, creation_date.

I pull all the posts in the code and then send it to the template. I want to format the creation_date field so it's formatted a bit nicer. Something like M-d-Y. I know I use strptime or strftime to format the creation_date. But I'm not sure how to do it before I send posts to the template.

Here is what I use to get the posts and send it to the template...

class HomeHandler(BaseHandler):
    def get(self):
        posts = Post.all()
        posts.order("-creation_date")
        self.render('home.html', posts=posts)

UPDATE:

posts = Post.all().order("-creation_date").fetch(50)
posts = [{'text': post.text} for post in posts]
for post in posts:
        print post.text

Error message I get:

AttributeError: 'dict' object has no attribute 'text'

+1  A: 

Assuming you are using Tornado's template module, it includes the datetime module. I have not used Tornado's template module, but you should be able to use:

entity.datetime_property.strftime('%m-%d-%y')

If you want to process your models before sending them to the template try something like:

class HomeHandler(BaseHandler):
  def get(self):
    posts = Post.all().order("-creation_date").fetch(50)
    posts = [{'author': post.author,
              'subject': post.subject,
              'date': post.date} for post in posts]
    self.render('home.html', posts=posts)

Within your template posts will be a list of dictionaries containing the fields author, subject, and date.

Use fetch to limit the number of posts you return; it will improve performance by grabbing (up to) 50 entities at once instead of grabbing them in smaller batches.

Robert Kluin
Awesome. Both ways worked for me. Thanks!
TylerW
Doh, I lied! I thought I got the second part to work, but I was wrong. I'm getting "AttributeError: 'dict' object has no attribute 'text'"
TylerW
Can you post the actual code you used in both the handler and the template?
Robert Kluin
Sorry, I'm still kinda a newbie to this site... I tried to put the code in the comment. I just updated the original post with some bare bones version of the problem.
TylerW
try: print post['text']
Robert Kluin
Okay. That prints it out. Now I gotta figure out how to use post['text'] in the template. It's still all pretty confusing to me though. When I do posts = Post.all(), and I type "print posts", I get "<google.appengine.ext.db.Query object at 0x4748dd0>". But when I cycle through it in a loop, "print post.text" works. And then when I add the line of code you gave me, I can do "print posts" and it prints out dictionary within dictionary?
TylerW
The code I gave you will let you processes the posts in your handler before sending them to a template. In this case it is done using a list comprehension (http://docs.python.org/tutorial/datastructures.html#list-comprehensions). Here is the confusing part, I think to access the dictionary element *within a django template* you use "dot notation, ie dictionary.element. If you just want to loop over all of the entities in your template, just omit the list comprehension (the posts = [...] part).
Robert Kluin