I’m trying to configure Django to serve static files when using runserver (production works fine). Everything works fine for all of the static files that are under an apps directory. The problem comes with static files that are not under a specific app, but are in the final static directory. For instance, I have this project structure:
/myproject/
/myproject/static/
/myproject/static/css/foo.css
/myproject/app1
/myproject/app1/static/css/bar.css
urls.py
if settings.SERVE_STATIC:
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
)
urlpatterns += staticfiles_urlpatterns() # one of these may be redundant.
settings.py
SERVE_STATIC = True
PROJECT_ROOT = '/myproject'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(PROJECT_ROOT, 'static'),)
INSTALLED_APPS = ('app1',)
With these settings, I get the error:
ImproperlyConfigured: The STATICFILES_DIRS setting should not contain
the STATIC_ROOT setting
Which makes sense. I’m telling Django to collect static files, and put them in the same place – which could cause a loop.
If I comment out the STATICFILES_DIRS variable, django will find the static file ‘bar.css’. But it does not find ‘foo.css’.
If I comment out the STATIC_ROOT variable and put back the STATICFILES_DIRS, then it finds the file ‘foo.css’ – but of course, the ‘collectstatic’ command will no longer work.
Note – I realize that the ‘/static’ directory is supposed to be empty, but the project I’m on, has files there anyway. 🙂 As long as they’re not overwritten by ‘collectstatic’, it looks like Django runserver should serve them – but it doesn’t.
How do I serve the static files under STATIC_ROOT (such as foo.css) when running Django runserver?
Move the files that are in /static/ right now to a different directory — call it /project-static/, for instance.
Then only include this line in urls.py:
(remove the django.views.static.serve view)
And in settings.py, use this:
Then you can put files in /project-static/ directory on your filesystem, the development server will serve them out of the /static/ URL prefix, and in production,
collectstaticwill find them and put them into the /static/ directory where the web server can find them.