django-static-files

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.

# 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

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:

# 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

Found base.css here:

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 {{ MEDIA_ROOT }}.

# this will result in the following:

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.

References

http://django-staticfiles.readthedocs.org/en/latest/index.html