-
Notifications
You must be signed in to change notification settings - Fork 19
Controllers
According to the MVC specification which whirlwind is based upon, the Controller should handle the business logic. The Controller receives and responds to requests from the client. The user's requests for pages are routed to some subclass of the python class BaseRequest using the @route decorator.
Here's an example implementation of a controller that handles "static page" requests:
from whirlwind.core.request import BaseRequest
from whirlwind.db.mongo import Mongo
from tornado.web import authenticated
from whirlwind.view.decorators import route
@route('/')
class IndexHandler(BaseRequest):
def get(self):
#template context variables go in here
template_values = {}
# you can do some database manipulation here
# or not do anything at all
# render the template defined in application/views/site/index.html
self.render_template('/site/index.html',**template_values)
The above controller code just handles rendering the root page. This page can be in its own file: sites_controller.py.
We suggest that the Request Handlers (which are subclasses of BaseRequests) be categorized according to functionality. In other words, Request Handlers that handle static requests (that aren't related to any other indepth functions) can be placed together in sites_controller.py in the controllers folder. Handlers that handle requests to login and logout of the application can be placed in an accounts_controller.py. And so on...
The BaseRequest class is defined in the whirlwind python source. It's a subclass of the RequestHandler class that's part of the tornado python web server.
Method : template_exists
Params : template_name - name of template
Returns : true if the template defined at template_name exists. Else returns false`.
Method : render_string
Params : template_name - name of template
**kwargs - dictionary of options
Returns : String that's the result of pre-processing the template at template_name.
Alias : render_to_string
Method :
The BaseRequest class has several methods that are useful in rendering template file and template strings and Base Requests can be based on request
We suggest that the controllers Request Handlers be classified according to
@threaded
@authenticated
The controller sometimes handles requests by rendering a template or by just writing out JSON or plain HTML to the client Since the controller handles client requests, it