Django template can't see CSS files


Question

I'm building a django app and I can't get the templates to see the CSS files... My settings.py file looks like:

MEDIA_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'media')
MEDIA_URL = '/media/'

I've got the CSS files in /mysite/media/css/ and the template code contains:

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

then, in the url.py file I have:

# DEVELOPMENT ONLY
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': '/media'}),

but the development server serves the plain html (without styles). What am I doing wrong?

--

OK - I got it working based on what you folks have said. The answer is:

settings.py:

MEDIA_ROOT = 'd://web//mysite//media//'  #absolute path to media
MEDIA_URL = '/mymedia/' #because admin already using /media

site_base.html:

<link rel="stylesheet" type="text/css" href="/mymedia/css/site_base.css" />

urls.py

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

And voila! It works.

1
20
3/23/2016 9:55:14 PM

Accepted Answer

in the "development only" block in your urls.py you need to change

(r'^media/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': '/media'}),

to...

(r'^media/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': settings.MEDIA_ROOT}),
13
5/5/2010 11:23:23 PM

ADMIN_MEDIA_PREFIX is set to \media\ by default, and is probably 'stealing' the path. Change that setting, or use a different one for non-admin media - eg site_media or assets.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon