Hack to fix Django staticfiles on same host

I recently thought my hosting provider “must have done something” because my Django site suddenly lost all style sheets and other static assets. The pages rendered but they looked bare-bones, and trying to load my site’s main CSS file (/static/css/site.css) produced a page not found error.

After some research I noticed on https://docs.djangoproject.com/en/1.8/howto/static-files/#configuring-static-files “During development, if you use django.contrib.staticfiles, this will be done automatically by runserver when DEBUG is set to True (see django.contrib.staticfiles.views.serve()). This method is grossly inefficient and probably insecure, so it is unsuitable for production. See Deploying static files for proper strategies to serve static files in production environments.”

So I caused the problem myself when I noticed DEBUG=True in my settings.py file recently and changed it to DEBUG=False “because security“, and didn’t even think that could somehow break all static file links (but it did).

The Deploying static files page was written for folks with multiple servers, such as a separation between the dynamic content server (Django) and a static content server. Since I just have one shared hosting account though, the page didn’t really apply and left me scratching my head how to fix it. (The standard advice page for setting up Django static files on Apache/wsgi didn’t help since my hosting provider seemed to bypass Apache settings files for Django sites).

I eventually figured out a fast hack that solved the issue: In my /public directory which apparently Passenger/wsgi always checks first, I created a symlink to the static directory (~/domain.com/public# ln -s ../myapp/static). I reloaded my site and bam, fixed.

I’m going to try to post things like this in the future, to help out others with irritating technical problems, instead of just searching the Internet, finding something half-useful, playing around, solving something, and then moving on without “giving back” and helping the next person. Though I’ll admit I was somewhat inspired (and amused) by this XKCD:

XKCD.com/979

http://xkcd.com/979/