[albatross-users] Writing Headers

Matt Goodall matt at pollenation.net
Fri Jul 11 19:03:16 EST 2003


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

As it happens I played around with this just the other day. I probably 
don't understand it all but I have written a "page" that sends an image 
back to the browser which will probably get you going.

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

The above code is only an experiment but it should demonstrate the 
basics. Obviously, if you are returning non-HTML then you will need to 
set the "content-type" to the correct MIME type, not hard code it like I 
did (have a look at the mimetypes module).

If you are returning HTML and just need to set your own headers then I 
suspect you only need to call ctx.set_header() and leave the rest to 
Albatross, i.e.

    def page_display(ctx):
        ctx.set_header('blah-blah', '123')
        ctx.run_template('something.html')

You don't seem to need to worry about calling write_headers(), it's 
presumably handled for you by the context and request adapter.

Hope this helps.

Cheers, Matt

Sheila King wrote:

> OK, I'm sufficiently confused about the new method of handling headers 
> to remark upon it.
>
> In the application that I am working on (which I started with 
> Albatross 1.01), I needed to send custom headers of my own. Since 
> header handling has changed in 1.10, I need to fix my app.
>
> But I'm going over the new documentation and getting a bit confused.
>
> (P.S. you need to fix the copyright date in a lot of your files...as 
> it is only copyright 2001.)
>
> Anyhow, I'm trying to fix this up, and the documentation seems to have 
> some flaws/omissions or something.
>
> In section 7 (documentation for Mixins)
> http://object-craft.com.au/projects/albatross/albatross/mixin-ref.html
>
> there is no mention of the new ResponseMixin (this is a new Mixin 
> class, right?)
> Also, when I try to look up in the documentation index the 
> "write_headers()" method, I go to "w" and
> http://object-craft.com.au/projects/albatross/albatross/genindex.html
> #letter-w
>
> the "write_headers" method is listed there, but it is listed for all 
> the AppContexts from which it was removed? But it is not listed for 
> the new ResponseMixin class.
>
> When I search the new .pdf document for "set_header" it only appears 
> in the schematic diagrams for the pre-rolled classes (Chapter 8)
>
> Same with "write_header" except for one mention in Chapter 5 for the 
> Fake Application harness. Otherwise only mentioned in the schematic 
> diagrams of Chapter 8.
>
> I sure would like to see some examples of setting headers so I would 
> have a better idea how I ought to use this. At least, what I mean is, 
> the documentation would be more complete with such an example. I'm 
> sure I will figure it out eventually by reviewing the source code and 
> experimenting, but it would be nicer if I didn't have to.
>
> Anyhow, I will put this off until tomorrow. Am too tired to mess with 
> it any more right now.
>
> Good night,
>

-- 
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenationinternet.com
e: matt at pollenationinternet.com
t: +44 (0)113 2252500





More information about the Albatross-users mailing list