119👍
I implore you to read the howto docs here: http://docs.djangoproject.com/en/dev/howto/static-files/
In short: STATIC_ROOT is only used if you call the collectstatic manangement command. It’s not needed to add the directory to the STATICFILES_DIRS setting to serve your static files!
During development (when the automatic serving view is used) staticfiles will automatically look for static files in various locations (because you call its “serve” view with a path to a static file). For this search it’ll use the so called “finders” (as defined in the STATICFILES_FINDERS setting).
-
One of the default finders is the AppDirectoriesFinder, which will look in the “/static/” directory of each of the apps of yours INSTALLED_APPS setting.
-
Another default finder is the FileSystemFinder, which will look in directories you specify in the STATICFILES_DIRS setting.
BTW, both these search patterns are similar to how template loading works.
The same technique will be used when running the collectstatic command, except that it now will collect the files from the various locations (using the same finders as above), putting the found files into STATIC_ROOT, ready for deployment.
3👍
If you want just a solution – scroll down to the “Solution”.
Overview
I was discovering the same problem yesterday. Here is what I found:
All you need is appropriate static finder, that will find your STATIC_ROOT and all its contents, but there is no such finder. Here are default finders:
-
django.contrib.staticfiles.finders.AppDirectoriesFinder – search installed django applications dirs for ‘static’ folder, but most of them use obsolete ‘media’ folders for now.
-
django.contrib.staticfiles.finders.FileSystemFinder – use all dirs mentioned in the STATICFILES_DIRS, but you can’t add STATIC_ROOT into it.
-
django.contrib.staticfiles.finders.DefaultStorageFinder – search static in your DEFAULT_FILE_STORAGE which is django.core.files.storage.FileSystemStorage by default and it points to your MEDIA_ROOT
Solution
That’s all, no additional choices. There are no choices to use STATIC_ROOT for now (in Django 1.3).
So I’ve just wrote my own custom finder for these needs:
-
My custom static finder file: finders.py:
from django.core.files.storage import FileSystemStorage from django.contrib.staticfiles.finders import BaseStorageFinder from django.conf import settings class StaticFinder(BaseStorageFinder): storage = FileSystemStorage(settings.STATIC_ROOT, settings.STATIC_URL)
-
settings.py:
STATICFILES_FINDERS = ( 'finders.StaticFinder', )
- If you want to use it with another finders – I suggest to put them after it inside STATICFILES_FINDERS
And remember: this solution should be used only in development needs and never on production!
- [Django]-Django gives Bad Request (400) when DEBUG = False
- [Django]-Loading fixtures in django unit tests
- [Django]-Is there a naming convention for Django apps
3👍
I found a quick and easy workaround to serve project global static files during development:
- Start a new app that contains your projects static files (e.g. “manage.py startapp static_content”)
- Create a folder named ‘static’ in that app and put your static files there.
- Add your new app to the list of installed apps
- [Django]-How can I get the full/absolute URL (with domain) in Django?
- [Django]-Django REST Framework combining routers from different apps
- [Django]-How does it work, the naming convention for Django INSTALLED_APPS?
1👍
First of all, make sure that you have static serve enabled in your urls.py FOR DEVELOPMENT ONLY.
Also, you may have an extra slash in there. If your STATIC_URL == ‘/static/’, then
{{ STATIC_URL }}/css/style.css should be {{ STATIC_URL }}css/style.css.
- [Django]-OneToOneField and Deleting
- [Django]-Django error: render_to_response() got an unexpected keyword argument 'context_instance'
- [Django]-Django REST Framework viewset per-action permissions
1👍
STATIC_ROOT = '/home/ws/be/bla/'
STATICFILES_DIRS = ('/home/ws/be/static/',)
STATIC_URL = '/static/'
This works for me. STATIC_ROOT must differ from STATICFILES_DIRS (Django version 1.4 pre-alpha SVN-16436)
- [Django]-Testing admin.ModelAdmin in django
- [Django]-Altering database tables in Django
- [Django]-Data Mining in a Django/Postgres application