开发者

I can't load the css files

开发者 https://www.devze.com 2022-12-27 23:51 出处:网络
I want to load some .css files to my Django project but I don\'t know why they aren\'t loaded. The css files are located at \"/myproject/media/css\".

I want to load some .css files to my Django project but I don't know why they aren't loaded. The css files are located at "/myproject/media/css".

settings.py:

import os.path
PROJECT_DIR = os.path.dir开发者_StackOverflowname(__file__)    
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')

urls.py:

from django.conf import settings
...
    (r'^media/(?P<path>.*)$', 'django.views.static.serve',
     {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),

)

base.html:

<link rel="stylesheet" type="text/css" media="all" href="{{ MEDIA_ROOT }}css/myStyle.css" />


Just a shot in the dark but if you have media root set to /myproject/media and then you reference {{ MEDIA_ROOT}}css/mystyle, aren't you missing a slash in there? So you are requesting /myproject/mediacss/myStyle

I feel you on troubleshooting this though. A pain.


MEDIA_ROOT is the file path to the assets, not the URL. The URL is set in urls.py, and you've got it as /media/, so that's what you need to use in the HTML tag.


Specifically, you should be using MEDIA_URL in your templates rather than MEDIA_ROOT.

0

精彩评论

暂无评论...
验证码 换一张
取 消