Django: how do you serve media / stylesheets and link to them within templates

I just had to figure this out myself.

settings.py:

MEDIA_ROOT = 'C:/Server/Projects/project_name/static/'
MEDIA_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/media/'

urls.py:

from django.conf import settings
...
if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
    )

template file:

<link rel="stylesheet" type="text/css" href="https://stackoverflow.com/static/css/style.css" />

With the file located here:

"C:/Server/Projects/project_name/static/css/style.css"

Leave a Comment