Make Donations

Recent site activity

References‎ > ‎GAEO Classes‎ > ‎

gaeo.controller.BaseController

Introduction

gaeo.controller.BaseController is used for request processing. In other words, once you want to process a HTTP request, you need to create a controller that extends gaeo.controller.BaseController and then the request will be dispatched to the proper method (action) in this controller. For example, the default root path (/) handler is the welcome controller and index action, you should have the following controller to handle the / request:

# application/controller/welcome.py
from gaeo.controller import BaseController

class WelcomeController(BaseController):
    def index(self):
        self.render(text='hello, world')

VERSION: 0.2.1

Class References

  • Member variables & Properties

  • cookies

A dict object that stores the browser cookies. If you want to set a 'foo' cookie, you can use

self.cookies['foo'] = 'blah...'

to store value.

It's an alias to the request.cookies.
  • has_rendered

A boolean value that tells if the template had been rendered or the render method has been invoked.
  • params

A dict object that stores every request parameters. For example, if you request the URL

http://example.com/?a=123&b=abc

then in your index action of welcome controller, the params would be

self.params == {
    'controller': 'welcome',
    'action': 'index',
    'a': '123',
    'b': 'abc'
}

So you can use self.params['a'] to get '123' .
  • request 

An alias to the google.appengine.ext.webapp.RequestHandler.request.
  • response

An alias to the google.appengine.ext.webapp.RequestHandler.response.
  • session

The GAEO's session implementation. It's a dict object that stores session data. It's almost similar with cookies object but you MUST invoke its put method to really store session data. For example,

self.session['foo'] = 123
self.session.put()
  • _controller

An alias to params['controller'].
  • _action

An alias to params['action'].
  • _is_mobile

A boolean object that tells if the client is a mobile browser (neither iPod nor iPhone)
  • _is_iphone

A boolean object that tells if the client is an iPod or iPhone.
  • _is_android

A boolean value that tells if the client is an Android device.
  • Methods

  • after_action(self)

Put your post process code here. This method originally is empty and you can override it in your controller class. This method would be invoked by the dispatcher after the actual action has been invoked.
  • before_action(self)

Same as after_action but the method used pre-processing.
  • invalid_action(self)

Once an unknown URL is requested, the dispatcher would invoke this method.
  • redirect(self, url, perm = False)

It's an alias to the google.appengine.ext.webapp.RequestHandler.redirect method but set True to has_rendered variable.
  • render(self, *html, **opt)

The main output method of a controller. If you don't invoke this method, the dispatcher will find the proper template to render. The render method supports many types of output and it help you output something without setting the Content-Type header (people often forget to set).

There are 2 ways using this method -- type-less and type-specified. In type-less usage, you can directly output a HTML string to the client. For example,

self.render('<p>hello, world</p>')

The string '<p>hello, world</p>' will be sent client in a 'text/html' content-type. The other usage, you should specify the type and options.
  • text
self.render(text='This is a plan text.')

The text will be sent client in a text/plain content-type
  • html
self.render(html='<h1>Hello</h1>')

The html string is sent in text/html type.
  • json
result = self.to_json({'a': 123, 'b': 'abc'})
self.render(json=result)

Output an JSON string in application/json type.
  • xml
self.render(xml='<el>An Element</el>')

Output an XML string in text/xml type.
  • template
self.render(template='edit')

Specify the template file for rendering. If the action is index, the dispatcher will find the index.html for rendering. You can use this method to specify another template file.
  • template_string
self.render(template_string='Hello, {{ nick }}',
            values={'nick': 'alice'})

To render an in-line template with values.
  • image
imgdata = ..... # processed with App Engine's Image API
self.render(image=imgdata)

To output a binary image data in image/<image_type> type.

Note that all the content-type is set to charset=utf-8.
  • to_json(self, obj, **kwds)

It's a shortcut from simplejson's JSONEncoder. You can put a dict object into this method and it returns a JSON string for you. For example,

obj = {'x': 123, 'b': 'abc'}
self.to_json(obj)  # returns "{'x': 123, 'b': 'abc'}"