[Fixed]-Making Django development server faster at serving static media

9đź‘Ť

âś…

Consider using mod_wsgi instead, and having httpd handle the static media.

5đź‘Ť

Development server is simple unsafe single-threaded application, so you cannot do much.

One trick you could try is to redirect /site_media to second development server, but this is ugly and probably wouldn’t help that much. So you could try bundling/compressing multiple assets into one css/js (e.g. using YUI Compressor).

And in any case, you should have separate static media server, that can serve multiple assets at once.

4đź‘Ť

Install Firefox (if you haven’t already), and install the Firebug Add-On. Restart your browser. In the lower-right corner click the “bug” icon and make sure that in the “Network” tab (it’s a dropdown) of the Firebug panel that opens at the bottom of the browser, the network monitor is active.

Now with the network tab of Firebug open, open your Django-generated page that you observed to load slowly. Take a look at the timeline bars. You’ll notice that the colored fragment(s) of each bar indicate(s) the reason for each request’s total “load” time. Violet, for instance, means that actually the browser is waiting for the server to generate the response. Gray means it’s receiving content. And so on. Hovering over the bars will display a color legend.

With Firebug’s network monitor you should be able to pinpoint how exactly your browser and/or server are spending their 10 seconds.

👤nikola

2đź‘Ť

Run lighttpd to serve the static content and use the MEDIA_URL to point the pages at the lighttpd server that servers the static stuff.

👤boatcoder

1đź‘Ť

You can try using django-extensions runserver_plus command with the --threaded option as a replacement for Django’s runserver command. Under the hood, it uses Werkzeug as the threaded WSGI server. You may notice a huge improvement in loading times for static files.

👤Flux

0đź‘Ť

Lightning fast ans easy on resources when using NGINX for serving static and media files. Here’s how it goes. However, you’ll need to adapt some paths according to your use case and system. But I think this will get you started:

1) Download NGINX for your system, in your case Windows:
http://nginx.org/

2) Unpack the zip file. Here’s how your NGINX config file may look like. This file lives inside nginx/conf/:

worker_processes 1;

events {
    worker_connections  1024;
}

http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    # root path to your project
    # use "..." if spaces are in the path
    # you may add a drive letter if required, e.g. root c:/foo
    # use / instead of \. It's simpler and works
    root /example/path/django/project/;
    server {
        # any free port number will do; Django dev server usually runs on port 80
        listen 8000 default;
        server_name localhost; # or 127.0.0.1
    }
}

3) Start NGINX by calling the nginx.exe – no options needed.

4) Tweak your Djnago project’s settings.py file:

if DEBUG:
    STATIC_URL = 'http://localhost:8000/static/'
    # against Django recommendation, I often still use the static
    # directory for user uploads; old-style Django ;-)
    MEDIA_URL = 'http://localhost:8000/static/uploads/'

Now, the static URLs in Django should look something like this:
http://localhost:8000/static/js/base.js

… 5) Stop the server by calling:

taskkill /f /IM nginx.exe

Well, that’s it. I typed this quickly, so just let me know if anything’s unclear or not working for you. I may improve this answer accordingly.

Leave a comment