Log in

Response object

Each Request Handler returns an instance of a Response object which contains the data sent in response to a web request.

To be able to create an instance of the Response object, simply import the Response variable from tipfy:

from tipfy import Response

Here, Response is an instance of werkzeug.Response.

Here's an example of the simplest use of Response, from the Hello, World! Tutorial:

from tipfy import RequestHandler, Response

class HelloWorldHandler(RequestHandler):
    """The simplest Tipfy handler example."""
    def get(self, **kwargs):
        return Response('Hello, World!')

Headers

The Response.headers contains the headers dictionary for the response. For example:

response = Response('Hello, World!')
response.headers['Content-Type'] = 'text/html'

There most common headers can also be assigned as data attributes (e.g., content_type) and through methods (e.g., set_cookie() ).

response = Response('Hello, World!')
response.content_type = 'text/html'
response.status_code = 200
response.set_cookie('cookie-name', value='cookie-val', path='/')

Data

The Response.data contains the data for the response. For example:

response = Response()
response.data = 'Hello, World!'

Here's an example of responding with image data.

response = Response()
response.data = image
response.headers['Content-Type'] = 'image/png'
return response

Though the above could instead be simplified as:

return Response(image, content_type='image/png')

Template Response

A template response is a Response that was filled from the results of rendering from a Jinja2 (or Mako) template. Here's the example from Hello, World! take 2

   from tipfy import RequestHandler
   from tipfy.ext.jinja2 import render_response

   class HelloWorldHandler(RequestHandler):
       def get(self, **kwargs):
           context = {'message': 'Hello, World!'}
           return render_response('hello.html', **context)

Powered by Moe. Yeah, the name is Moe. Powered by Google App Engine