Django serving static PDF file

1,234 views
Skip to first unread message

Arnold Chen

unread,
Jul 17, 2007, 3:26:14 AM7/17/07
to Django users
Can any one please tell me how to serve a static PDF in django ? The
file is located in the server, and do not need to be created on the
fly (by using report lab). I have done it in PHP by using header, but
i just don't know how to do it with django. Thanks

Ben van Staveren

unread,
Jul 17, 2007, 3:29:01 AM7/17/07
to django...@googlegroups.com
You're better off not doing it with Django, just make a directory
that won't be handled by Django and stick all your static content in
there. After all, the webserver is usually better at serving static
files than Django is :)

Arnold Chen

unread,
Jul 17, 2007, 4:03:15 AM7/17/07
to Django users
Thanks Ben,

Besides, i've found that www.lawrence.com and www.ljworld.com (which
are famous sites that use django) use a http://media.their-domain-name.com
to store the media files.
All static files, images, css are from the http://media.their-domain-name.com
server. Obviously the media subdomain is not a django environment, i
wonder if we can setup an environment like that, and can i still
upload image to the media subdomain from the django admin console?

Arnold

Arnold Chen

unread,
Jul 17, 2007, 4:29:49 AM7/17/07
to Django users
Besides, i want to revise my first question as:

How to serve static file by "pushing" a PDF to client? In php, there
is a way to serve PDF by pushing the file to the client, and client to
choose "Save As" or "Open" the file directly.

Adobe reader is very slow if they run in browsers, and most of the
time, they hang the browser, so is there a way to push ?

regards,
Arnold

On Jul 17, 4:03 pm, Arnold Chen <arn...@design97.com> wrote:
> Thanks Ben,
>
> Besides, i've found thatwww.lawrence.comandwww.ljworld.com(which

> are famous sites that use django) use ahttp://media.their-domain-name.com


> to store the media files.

> All static files, images, css are from thehttp://media.their-domain-name.com

Ben van Staveren

unread,
Jul 17, 2007, 4:40:07 AM7/17/07
to django...@googlegroups.com
Generally that's done by spitting out some custom headers;

Content-Type: text/pdf
Content-Disposition: attachment; filename=yourfilename.pdf

That way most browsers will give you the option to open or to save-
as. To do that does require that they're sent dynamically, but as
said before it'd be a shame to use Django for that because of the
overhead you incur. Better off to just write some mod_python module
that will handle that sort of things.

IMO, anyway :)

Sam

unread,
Jul 17, 2007, 4:47:11 AM7/17/07
to Django users
import urllib

from django.http import HttpResponse

def output_file(request, file, mimetype):
f = urllib.urlopen(file)
data = f.read()
f.close()
return HttpResponse(data, mimetype=mimetype)


It is better to serve from static environments but sometimes, you want
to check user rights for download and you can't avoid the django env.

Reply all
Reply to author
Forward
0 new messages