Note: Django Static Files (django-staticfiles) has been included as a contrib app in django 1.3. However, for the most cutting edge features, and also compatibility with earlier Django versions, users can install the ‘django-staticfiles’ app. django-staticfiles is a handy tool for managing and transitioning static media between development environments (which …
Note: Django Static Files (django-staticfiles) has been included as a contrib app in django 1.3. However, for the most cutting edge features, and also compatibility with earlier Django versions, users can install the ‘django-staticfiles’ app.
django-staticfiles is a handy tool for managing and transitioning static media between development environments (which we really recommend you use), and production environments (which we really recommend you not develop on).
Static media for large projects becomes difficult to manage when developers have to take into account media on a per-app basis, while also taking into account right directory placement in terms of version control, and while also being able to support per-developer requirements, such as making local-static-media serving more straightforward.
With django-staticfiles, the following becomes trivial:
* migrating media files to a single directory for web hosting
* hassle-free local developer static hosting with Debug = True
* support for media files being with versioned instances in their proper (app) directories.
Installation (and Usage)
As I was installing django-staticfiles (on a 1.2.5 django instance, which is still prevalent in the wild as of this writing), I encountered problems with configuration. Suffice it to say, django-staticfiles does not play nice with the hack introduced in the Django docs, as the urls can potentially conflict with the ones you choose to use. Also, the tutorial described in git, readthedocs, and Django documentation is missing one detail, described later in this article.
** Again, django-staticfiles is included as a contrib app in Django 1.3. use this for cutting edge updates or for Django versions 1.2 and under.
# Install via pip »> pip install django-staticfiles # Modify settings.py INSTALLED_APPS = [ # other installed apps ’staticfiles’, ] PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
# for cleanliness purposes, we include the main media folder in the project root,
MEDIA_ROOT = os.path.join( PROJECT_ROOT, ‘media/’ ) # basic media root folder, user may have multiple media folders each located within their respective apps.
# required settings for django-staticfiles, data will be placed here by ./manage.py collectstatic
STATIC_ROOT = os.path.join( PROJECT_ROOT, ‘static_media/’ )
MEDIA_URL = ‘/site_media/’
STATIC_URL = “/static/”
You’re almost done! Or, Some Caveats
* Make sure that your media folder in each app is named ‘static’ so that the automated finder will find it.
Things to note:
python manage.py collectstatic is a very useful tool, but there are some problems worth noting (based on my testing)
# Given the following:
STATICFILES_DIRS = ( (“generic_media”, MEDIA_ROOT), # site-wide media storage )
# and given an app containing a ‘static’ folder
# and MEDIA_ROOT and PROJECT_ROOT/myapp/static/ containing a file named ‘base.css’,
# Trying out using findstatic
»> python manage.py findstatic base.css
Found base.css here:
»> python manage.py findstatic generic_media/base.css
Found base.css here:
Note that the previous 2 commands performed as expected, with base.css in the given namespace generic_media prioritizes the one in .
»> python manage.py collectstatic
# this will result in the following:
# STATIC_ROOT/base.css — as located in PROJECT_ROOT/myapp/static
# STATIC_ROOT/generic_media/base.css — as located in PROJECT_ROOT/media/
Notice how it uses the namespace defined in STATICFILES_DIRS, which is in turn used in the url STATIC_URL/generic_media, which now, in turn, is used by the file system. It’s all intuitive, and makes perfect sense.