Tuesday, May 10, 2011

GAE - Template


Template makes your life easier. it separating between code logic of your program to what should display on the browser. Here's I just show up what the crucial template case that not handled by Django documentation.

Displaying List of Articles

It's really some often occurance for us to display item list. It's like blog post that consits of title, date, and content. Or it like personal data that consists of name, email, address and more. Here's for example we has some blog post:

Data Content

GAE TemplateGAE Template is use Django template system. So when you are accustomed for using Django, it's good for you.
GAE RuntimeThere are two available runtime for you. The first is Java and the second is Python. Whatever you choose, it should based on your preference for the language and your current skills.
GAE DatastoreIn GAE, your data is saved in database, but you never handle it directly, because Google Datastore that handles it, and in all your time, you handle the datastore. The operation for data in datastore is like in native sql, but it's more abstract than it that is called GQL - Google Query Language.
For example you want to display the data on the home page, what you need is you main the logic code that retrieves the data using Google datastore query.

Program code:

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext.webapp import template

class Blog(db.Model):     title = db.StringProperty()
    content = db.TextProperty()

class MainPage(webapp.RequestHandler):
    blog = Blog()
    query = db.GqlQuery("SELECT * FROM Blog")
    data = []
    for row in query:
        mapp = {}
        mapp['title'] = row.title
        mapp['content'] = row.content


No comments:

Post a Comment