Generic base controller used by all web requests.
The Controller class helps manage all web requests. This is done by all requests being an instance of this class, or a subclass thereof, see: isinstance().
Initialize the web request, performing tasks common to all web requests.
Parameters: |
|
---|
Default chula.www.adapters.env.BaseEnv.content_type is set to text/html if not specified.
Complete garbage collection. The intended purpose is to allow consolidated garbage collection specific to each project. This method gets called in the adapter just before sending data to the browser.
Warning
Do not call this method, it gets called automatically.
Provide mechanism for removing items from session just prior to being persisted. This is useful when you want to have unserializeable objects that need to be casted to a different type before being persisted to the database. This usually means casting to a json encodable type.
>>> from chula.www.controller import base
>>>
>>> class Foo(base.Controller):
... def _pre_session_persist(self):
... self.database_connection.close()
... del self.database_connection
>>>
Provide a consistent method name for execution by the handler. This method is to be rebound by the UrlMapper.
Warning
Do not call this method, it gets called automatically.
Redirect the browser to another page.
Parameters: |
|
---|---|
Return type: | str |
Currently the supported redirection types are:
Example of how to perform a redirection
>>> from chula.www.controller import base
>>>
>>> class Foo(base.Controller):
... def foo(self):
... url = '/some/other/page'
... return self.redirect(url)
>>>
Note
This method needs to support integer values as well.
Attribute holding the chula.www.adapters.env.BaseEnv.content_type. Mutating this value will determine the outgoing HTTP Content-Type.
Here’s how you would set the content type inside a controller:
>>> from chula import json
>>> from chula.www.controller import base
>>>
>>> class Foo(base.Controller):
... def bar(self):
... self.content_type = 'application/json'
... payload = {'testing':'hello world'}
... return json.dumps(payload, indent=2)
>>>
Attribute holding the chula.config.Config object.
Attribute holding the chula.www.adapters.env.BaseEnv object.
Example of how to reference GET variables:
>>> from chula.www.controller import base
>>>
>>> class Foo(base.Controller):
... def http_get_variables(self):
... html = []
... html.append('<ul>')
... for key, value in self.env.form_get.iteritems():
... html.append('<li>%s = %s</li>' % (key, value))
... html.append('</ul>')
... return ''.join(html)
>>>
Example of how to reference POST variables:
>>> from chula.www.controller import base
>>>
>>> class Foo(base.Controller):
... def http_get_variables(self):
... html = []
... html.append('<ul>')
... for key, value in self.env.form_post.iteritems():
... html.append('<li>%s = %s</li>' % (key, value))
... html.append('</ul>')
... return ''.join(html)
>>>
Example of how to reference raw HTTP POST data:
>>> from chula.www.controller import base
>>>
>>> class Foo(base.Controller):
... def http_get_variables(self):
... return self.env.form_raw
>>>
Attribute referencing chula.www.adapters.env.BaseEnv.form. Rember this attribute is just a pointer to the combined form attribute, if you need more control reference one of the form_foo attributes in chula.www.adapters.env.BaseEnv.form.
Example of how to reference form variables:
>>> from chula.www.controller import base
>>>
>>> class Foo(base.Controller):
... def form_variables(self):
... html = []
... html.append('<ul>')
... for key, value in self.form.iteritems():
... html.append('<li>%s = %s</li>' % (key, value))
... html.append('</ul>')
... return ''.join(html)
>>>
Attribute holding chula.logger.Logger object. The intended use of this attribute is to allow app specific logging. The default configuration will send error messags to chula.config.Config.log and debug messages (and higher) to chula.config.Config.log suffixed with .debug
Attribute holding chula.collection.Collection object. The intended use of this attribute is to populate with data, and pass it to the view.
Attribute holding an instance of chula.session.Session object.
Note
This attribute only exists if chula.config.Config.session is True
Note
This attribute is also automatically exposed to the model attribute.