Advanced Topics

Returning Binary Data

By default, Keystone assumes a Content-Type of text/html for responses generated from .ks views, and requires a valid template section which must produce UTF-8 output. You can override this behavior on a per-view basis with return_response(), which bypasses the template entirely.

fp = file('/tmp/generated_file.pdf', 'rb')
header('Content-Type', 'application/pdf')
return_response(fp)
----

The body argument to return_response() may be a string or any iterable type (list, generator, file object) which yields strings.

Note

As of Keystone 0.2, you must still have a template section in your .ks file when using return_response(), though it may be empty. This may change in a future version of Keystone.

Table Of Contents

Previous topic

View Variables

Next topic

Deploying Keystone

This Page