Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django template can't see CSS files

Tags:

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.

like image 641
Technical Bard Avatar asked Jul 02 '09 17:07

Technical Bard


People also ask

How can I get CSS file in Django?

Including CSS in Django Template We need to load the static files by adding the below line of code at the top of the template (. HTML) file. Then, insert the stylesheet in the HTML using link HTML tag. If you are new to the HTML and CSS, you can learn more about adding CSS in HTML code.

Why template does not exist Django?

Django TemplateDoesNotExist error means simply that the framework can't find the template file. To use the template-loading API, you'll need to tell the framework where you store your templates. The place to do this is in your settings file ( settings.py ) by TEMPLATE_DIRS setting.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

Where should template folder be in Django?

Generally, the templates folder is created and kept in the sample directory where manage.py lives. This templates folder contains all the templates you will create in different Django Apps. Alternatively, you can maintain a template folder for each app separately.


2 Answers

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}),
like image 173
Jiaaro Avatar answered Sep 23 '22 02:09

Jiaaro


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.

like image 34
Daniel Roseman Avatar answered Sep 23 '22 02:09

Daniel Roseman