4
urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'core.views.homepage', name='homepage'),
    url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)

That's my urls.py

The static file works if I disable the DEBUG, and doesn't work If I turn it back on.

Part of my settings

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'

# Additional locations of static files
STATICFILES_DIRS = (

    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

Really strange to me, can anyone help?

3
  • Sounds odd. Especially since static files only work out of the box with debug enabled. I think the recommended way to do it is to use mod_rewrite, mod_alias or similar on the server (where debug = false) and simply let it handle it in the background on the dev machine (where debug = true). Commented Nov 2, 2011 at 19:29
  • I worked it out, but I want another solution here. The STATIC_ROOT does not served by django at all, you have to put in STATICFILES_DIRS. And when in debug off mode, you run collectstatic to copy those files to another folder. Commented Nov 2, 2011 at 19:36
  • That was annoying, cos if I want to test in non-debug mode, I have to run that command to refresh my files. Commented Nov 2, 2011 at 19:36

1 Answer 1

24

No, STATIC_ROOT is not served by Django ever. In production (debug off), it is expected that your web server will serve this directory directly. In development (debug on), you shouldn't have this directory or anything in it, anyways.

Let me say that again for emphasis. You are never supposed to directly save any assets in STATIC_ROOT. This directory is solely for the output from the collectstatic management command. All assets in your project are supposed to be saved in the static directory of the particular app it belongs to.

Now, of course, you'll often have assets that are not directly related to a single app, but rather your entire project as a whole. For this scenario, you create a separate directory in your project and place all common assets there. You then add this directory to the STATICFILES_DIRS setting.

In development, Django will serve anything in that directory, and in production, the collectstatic management command will pull assets from that directory into STATIC_ROOT.

Sign up to request clarification or add additional context in comments.

3 Comments

This is the single best explanation of this problem that I've seen. Many thanks Chris. +1 from me.
So in development, if you don't put things there, how are you supposed to serve the files that are going to be put into staticroot later? Manually set up a url pattern to serve another_dir/* when people visit static/*? The reason I think so many people get tripped up on this is because django blithely serves files from STATIC_ROOT, so most people don't set up local ways to serve files from other directories. Then when they switch to serving through apache, they just leave all the files there and never run collectstatic.
It's a common misconception. Django never serves files from STATIC_ROOT. If you use runserver with DEBUG=True, Django will serve up anything in each app's static directory and any directories specified in STATICFILES_DIRS at STATIC_URL. (In some scenarios you may need to add urlpatterns += staticfiles_urlpatterns() to urls.py). In production, you run collectstatic and your webserver handles it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.