Thursday, January 6, 2011

Pinax Static Media

Pinax 0.7 uses the django app called staticfiles to manage...well... static files. This app is installed in the virtualenv site-packages directory.

The thing that makes this app a little hard to understand at first is it's not very DRY. The way it works is you scatter your static files all over your django project (with rules about where each dir should be). In your settings.py file you create a variable called STATICFILES_DIRS. It looks something like this:

STATICFILES_DIRS = (
('pinax', os.path.join(PINAX_ROOT, 'media', PINAX_THEME)),
('skatepark', os.path.join(PROJECT_ROOT, 'media')),)

Then you run:

python manage.py build_media --all

Note this command is different in version 0.9. This command goes to each dir and copies all the subdirs and files into the dir specified by the settings.py variable STATIC_ROOT. For example:

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

I guess in the end, putting all your static files in one place makes it easier to server them on the production server. At this point in my project, the value of the staticfiles app is it makes it easy to over-ride default static media. In my case I wanted to over-ride the pinax logo.

The default was at:

/home/me/envs/skatepark/lib/python2.7/site-packages/pinax/media/default/pinax/images/logo.png

I put the logo I wanted in my project dir:

/home/me/skatepark/media/pinax/images/logo.png

The command

python manage.py build_media --all

started with the first dir in STATICFILES_DIR being copied and the default logo being written to:

/home/chuck/skatepark/site_media/static/pinax/images/logo.png

then the next dir in STATICFILES_DIR was scanned and the default logo was overwritten by my logo.

No comments:

Post a Comment