Django template can't see CSS files

21,598

Solution 1

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}),

Solution 2

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.

Solution 3

On the dev server, I like to cheat and put the following in my urls.py

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

That way anything in the project under the "/includes" folder is server by the dev server. You could just change that to "/media".

Solution 4

It also worked for me, thanks guys !!

settings.py

MEDIA_ROOT = '/home/pi/ewspaces/ws-classic/xima/media'

MEDIA_URL = '/statics/'

urls.py

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

inside templates:

<link type="text/css" href="/statics/css/base/jquery.ui.all.css" rel="stylesheet" />
Share:
21,598
Technical Bard
Author by

Technical Bard

Updated on March 24, 2020

Comments

  • Technical Bard
    Technical Bard about 4 years

    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.

  • Paul
    Paul about 13 years
    Thank you Daniel! I was pulling my hair out already! It's not the first time that I found your answers to django questions very helpful, so I guess it's just a big thank you.
  • d.putto
    d.putto over 11 years
    this also worked for me but I am wondering what do i need to change when I will deploy project on web.