Demo application The web application that we will be making for this demo will be a simple guestbook sort of application whereby any visitor can leave a comment on the webpage. In the application, we will have just two requests: one to render the page and other to record what the visitor comments on the page. To start creating the application, first create a directory ?guestbook? and all the files for the application will reside in this directory. The App Engine application requires a configuration file called app.yaml. This yaml file describes which URLs will be used by which handler scripts. The following code snippet shows the yaml file for our application. The application identifier ?guestbook? is the name of the application and we specify all URLs to be handled by the main.py script.
application: pcqdemoapp version: 1 runtime: python api_version: 1 handlers: - url: .* script: main.py
To upload the web application online on App Server appcfg.py command is used. And when prompted for email and password, pass your email id.
Webapp and datastore Since the application has to render web pages we have to use webapp framework which will handle the requests for such tasks. Therefor we will have to import WSGI handlers to handle the requests and the same will also be used to call Django template. While the datastore API lets you store persistent data and then retrieve it throughout your application. The code of the application looks like following and name the code file as ?main.py?.
#!/usr/bin/env python import wsgiref.handlers from google.appengine.ext import db from google.appengine.ext import webapp from google.appengine.ext.webapp import template class GuestBook(db.Model): message = db.StringProperty(required=True) time = db.DateTimeProperty(auto_now_add=True) sender = db.StringProperty() class MyHandler(webapp.RequestHandler): def get(self): guestbooks = db.GqlQuery('SELECT * FROM GuestBook ORDER BY time DESC') values = { 'guestbooks': guestbooks } self.response.out.write(template.render('main.html',values)) def post(self): guestbook = GuestBook( message=self.request.get('message'), sender=self.request.get('sender')) guestbook.put() self.redirect('/') def main(): app = webapp.WSGIApplication([(r'.*', MyHandler)], debug=True) wsgiref.handlers.CGIHandler().run(app) if __name__ == "__main__": main()
The class GuestBook defines the three attributes for the database structure. And using GqlQuery on database, we can retrieve the contents of the database. The class MyHandler inherits from webapp.RequestHandler and therefore in the GET method we will render the page request. With response.out we will pass the values retrieved from the datastore but also a template designed that will be used to display the output in a specified format, in our case it will be ?main.html? file. Similarly, for the POST method we will capture the content from the user's request, i.e. from the textboxes named message and sender, and pass the value to variable which is of type GuestBook.
In the above code snippet, we have to redirect the user to same page, so we will use self.redirect('/') in the POST method, which will return the request onto the same page webpage after rendering the result from the GET method also.
Get most out of your technology infrastructure investments with Dell
About CIOL | Media Kit | Site Map | Contact Us | Help | Write to us | Jobs@CyberMedia | Privacy Policy
Copyright © CyberMedia India Online Ltd. All rights reserved. Usage of content from web site is subject to Terms and Conditions.