Create Model class with name message.py
from google.appengine.ext import db class Message(db.Model): content = db.TextProperty(required=True) who = db.StringProperty() when = db.DateTimeProperty(auto_now_add=True) |
create main.py
class MainHandler(webapp.RequestHandler): def get(self): messages = db.GqlQuery('SELECT * FROM Message ' 'ORDER BY when DESC').fetch(100) values = {'messages': messages} self.response.out.write( template.render('list.html', values)) def main(): application = webapp.WSGIApplication([('/', MainHandler)], debug=True) wsgiref.handlers.CGIHandler().run(application) if __name__ == '__main__': main() |
list.html
<head><title>..............</title> <style> .even { background-color:#DDDDDD } .odd { background-color:#FFFFFF } </style> </head> <body> <table width="100%" cellspacing="0" cellpadding="0" > {% for message in messages %} <tr class="{% cycle even,odd %}" > <td colspan="3"> <table width='100%' border=1> <tbody> <tr class="{% cycle even,odd %}"> <td width="20%" valign="top">Nick</td> <td align="right" width="20%" valign="top">{{message.when}}</td> </tr> <tr height="40"> <td width="20%" valign="top">{{message.who}} </td> <td width="80%" valign="top">{{message.content}}</td> </tr> </tbody> </table> </td> </tr> <tr height="5px"><td colspan='3' align='center'> </td></tr> {% endfor %} </table> </body> </html> |
create app.yaml
application: template version: 1 runtime: python api_version: 1 - url: /.* script: main.py |
What the above code does
- In meesage.py file we define a model Message with three properties: content,who and when which plays a role of domain object and a db.This is part of App Engine not a python and It uses App Engine big table infrastructure which takes care of all of the distribution, replication and load balancing.
for detail refrence of entities and model visit http://code.google.com/appengine/docs/datastore/ - In main.py we define MainHandler class which serve as request handler of all get requests.We use GQL in our code for getting all message model objects. After fetching all results we assign these meesage to values and reponse back to list.html page. self.response.out.write use for writing reponse and template.render method use for rendring list template.
- After that we create html file with the name of list.html .In this html file we use Django script for manupolation Message objects. {% for message in messages %} is just foreach like code structure and "{% cycle even,odd %}" uses for applying style class alternatively each time. you can use any number of values, separated by commas for alternatively.
No comments:
Post a Comment