How to serve any static directory?

35 views
Skip to first unread message

Yann Salaün

unread,
Sep 1, 2015, 10:37:43 AM9/1/15
to django...@googlegroups.com
Hi all,

I'm working on an application where we want the following to happen:

1. The administrators upload zip archives containing web content (whole static websites) through admin interface
2. The archive is unzipped in the background to a random directory
3. The static content is served at a url that looks like http://<host>/site/<pk>/, with the index.html at the root, all the paths to css and js files, etc.

I know how to configure the route, but I was wondering what to put in the view. I have tried with STATIC_{ROOT,URL} and MEDIA_{ROOT,URL} but I feel it's not the right way.

In summary : is there a way to put something like `return serve_this_static_directory()` in the view? or is there any workaround there?

Thanks for your answers

Yann

Florian Schweikert

unread,
Sep 1, 2015, 10:43:44 AM9/1/15
to django...@googlegroups.com
On 01/09/15 16:34, Yann Salaün wrote:
> In summary : is there a way to put something like `return
> serve_this_static_directory()` in the view? or is there any workaround
> there?

Not sure if I get you right, you want to serve static files with django?
Is there any reason why it's not possible to serve them with nginx/apache?

regards,
Florian

signature.asc

Yann Salaün

unread,
Sep 2, 2015, 11:44:27 AM9/2/15
to django...@googlegroups.com
Hi,

The main reason I try to serve those static files with django is because I would like to use django to configure the route. 

Trying to clarify my question:

1. The url (http://<host>/site/<user-friendly-url>/) doesn't match the directory name (which could be anything, like `XYZ_html/`).
2. Administrators must be able to upload new archives, which should be extracted and served automatically. 

For those two reasons, I don't have any idea how to serve this content using the webserver, because how is it possible to configure the routes then?

Thank you for your answer


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/55E5B97B.3070607%40ist-total.org.
For more options, visit https://groups.google.com/d/optout.

Remco Gerlich

unread,
Sep 3, 2015, 6:21:18 AM9/3/15
to django...@googlegroups.com
Do you use a webserver "in front" of Django, like Nginx or Apache?

What we do:
- In *development* (when DEBUG=True), use django.views.static.serve to serve a file: https://docs.djangoproject.com/en/1.8/ref/views/#django.views.static.serve

- In *production*, let Django do what it needs to do (like authorisation, or in your case the dynamic translation of the URL to a file path) and then send headers to the webserver to let it return the file, freeing up the Django worker.

Under Nginx, we have a bit of nginx.conf in the server {} part like:

    location /protected/ {
          internal;
          alias /the/directory/root/of/the/files/
    }

And we create a path that starts with /protected/. For Apache, you simply need the full absolute path.

Then do

    response = http.HttpResponse()
    response['X-Sendfile'] = full_absolute_path  # Apache
    response['X-Accel-Redirect'] = path_starting_with_protected  # Nginx
    response['Content-Type'] = ''  # So the webserver will decide it
    return respone

Hope this helps.


Reply all
Reply to author
Forward
0 new messages