Wednesday, March 19, 2008

Django Development Server Style Sheet Configuration

When using style sheets (css), specially when using the Django development server, some adaptations are needed in get both the style sheets and the images served. For this, we need use the media path as defined for the framework.

settings.py
First the media path must be defined and must differentiate it self:

MEDIA_ROOT = '/path/to/media/'
MEDIA_URL = ''
ADMIN_MEDIA_PREFIX = '/amedia/'

urls.py
Now we add/link the path to the media path itself:

if settings.DEBUG:
urlpatterns += patterns('', (r'^media/(?P.*)$', 'django.views.static.serve', {'document_root':'/home/fbenavides/Documents/workspace3dot3/txm/src/txm/media/', 'show_indexes': True}), )

index.html
In the given HTML file header (index.html is just an example file), the style sheet link must be added:

<link href="/media/css/bluebusiness.css" rel="stylesheet" type="text/css" />

It is assumed that the media path is as follows:

/media/
/media/css/
/media/images/

Thus when calling other style sheets and/or images, these must follow the proper call path.

2 comments:

JKK said...

Thanks a lot!

JKK said...

In the lines
urlpatterns += patterns('', (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root':'/home/fbenavides/Documents/workspace3dot3/txm/src/txm/media/', 'show_indexes': True}), )
The <path> is not escaped so disappears..