Variations of this question have been asked, but I'm still unable to get my stylesheets to load correctly when my templates are rendered.
I'm attempting to serve static media from the Django process during development - which is strongly discouraged in production, I'm aware. I'll post my configuration and my template, and hopefully someone can help me to understand where I'm going wrong.
Note that I did try to follow the example on the Django project website, however it doesn't mention how to refer to your stylesheets from a template. I've also tried many different variations of the same thing, so my code/settings may be a little off from what's described.
settings.py
MEDIA_ROOT = 'D:/Dev Tools/django_projects/dso/media'
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/media/'
urls.py
from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/(.*)', admin.site.root),
(r'^ovramt/$', 'dso.ovramt.views.index'),
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
Within my template:
<head>
<title> {% block title %} DSO Template {% endblock %} </title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<link rel="stylesheet" type="text/css" href="../media/styles.css">
</head>
I assure you, the files (templates/media) are in the correct directory on my file system. If there's any extra information I need to provide, please post a comment.
Edit:
One of the problems I was having was the use of a '/' prepending my links. If the forward slash is prepended, the link is opened from the root of the site. If there is no forward slash, the link is opened in the current level. An example:
www.example.com/application/ has a link "/app2/ and a link "app3/".
app2 will open at www.example.com/app2/ and app3 will open at www.example.com/application/app3/. This was confusing me I think.
I usually make my own Template simple tag because Django isn't giving CSS/JavaScript files. Apache does it so my media url is usually http://static.mysite.com.
yourApp/templatetags/media_url.py:
And in my template file:
You could also make your own context preprocessor to add the media_url variable in every template.
Another thing to add is that if you have a separate media server on a subdomain/different domain, you can disable cookies for your static media. Saves a little processing and bandwidth.
I just use absolute naming. Unless you're running the site in a deep path (or even if you are), I'd drop the
..
and go for something like:I just had to figure this out myself.
settings.py:
urls.py:
template file:
With the file located here:
I've got a couple of ideas, I don't know which one of them is working for me :)
That's from http://docs.djangoproject.com/en/dev/ref/settings/#admin-media-prefix
Secondly, it may be that you're confusing directories on your filesystem with url paths. Try using absolute urls, and then refine them down.
Django already has a context process for MEDIA_URL, see Django's documentation.
It should be availbale by default (unless you've customized CONTEXT_PROCESSORS and forgot to add it) in a RequestContext.