Maybe it's old/or and documented[1] but yesterday i was looking for an easy

way to serve static files (for istance css,images,etc.) with Django using the development webserver.

After a quick search i found a solution

(ok maybe it's not the _best_ way to do it but works4me!).

Say that you want to serve images and css in with two different path,

add the following:

settings.py:

#[..]

STATIC_IMG_ROOT = '/fullpath/to/mycontent/img/'

STATIC_CSS_ROOT = '/fullpath/to/mycontent/css/'

#[..]

urls.py:

#[..]

(r'\^img/(?P\<path>.*)\$', 'django.views.static.serve',

{'document_root': settings.STATIC_IMG_ROOT}),

(r'\^css/(?P\<path>.*)\$', 'django.views.static.serve',

{'document_root': settings.STATIC_CSS_ROOT}),

#[..]

Now in your templates you can link to images and css via:

[..] awesome_header.png" width="137" height="121" [..]

and

[..] rel="stylesheet" type="text/css" href="css/main.css" [..]

Remember to disable these settings in settings.py and urls.py when you are going in production.

[1] http://docs.djangoproject.com/en/1.0/howto/static-files/

Comments