[albatross-users] Writing Headers

Dave Cole djc at object-craft.com.au
Sat Jul 12 13:16:45 EST 2003


>>>>> "Matt" == Matt Goodall <matt at pollenation.net> writes:

Matt> You are right, this stuff is handled by the ResponseMixin which
Matt> is included by the AppContext class so it should be there for
Matt> all of the standard context classes. Since there is no
Matt> documentation for this at the moment you are best off looking at
Matt> the code for more detail, it is in app.py and is very simple.

Ooops - I completely forgot about documenting ResponseMixin.  I will
do that today.

The ResponseMixin was introduced to simplify the process of sending
different types of content to the browser.  Matt is 100% on the money
with his examples.

Matt>     def page_display(ctx):
Matt>         f = None
Matt>         try:
Matt>             f = file('/home/matt/public_html/test/assets/images/%s' %
Matt>                      ctx.locals.file)
Matt>             img_data = f.read()
Matt>         finally:
Matt>             if f:
Matt>                 f.close()
Matt>         ctx.set_header('content-type', 'image/png')
Matt>         ctx.send_content(img_data)

Matt> The above code is only an experiment but it should demonstrate
Matt> the basics.

It is more than an experiment.  It is a perfect example of how to
manipulate the headers.

One subtle thing we did in relation to this was to make sure that the
session is saved before the output is flushed to the browser.  This
allows you to do things like include non-inline dynamically generated
images inside pages served to the browser.

Matt> If you are returning HTML and just need to set your own headers
Matt> then I suspect you only need to call ctx.set_header() and leave
Matt> the rest to Albatross, i.e.
Matt> 
Matt>     def page_display(ctx):
Matt>         ctx.set_header('blah-blah', '123')
Matt>         ctx.run_template('something.html')
Matt> 
Matt> You don't seem to need to worry about calling write_headers(), it's
Matt> presumably handled for you by the context and request adapter.

Yes.  Excellent summary.

- Dave

-- 
http://www.object-craft.com.au




More information about the Albatross-users mailing list