Sunday, 24 April 2011

Using CSS files in Django

In Django, cascading style sheets are served as static content. To begin with create the following URL mapping in your urls.py


from django.conf import settings


urlpatters = patterns (
   #other patterns
   (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
)


In settings.py set the following variables.

MEDIA_ROOT = '/home/user/site/static'
STATIC_URL = '/css/'

All css files are put in the css folder and can be referred to in templates as /static/css/filename.css.

No comments: