referencing css files with django
I'm new to the Django framework and am having a little bit of trouble following the documentation for including CSS files.
I have set up a media root in settings.py MEDIA_ROOT = '/home/daniel/django/site1/media/'
MEDIA_URL = '/static/'
And within that I have my css f开发者_如何学编程older and file
/home/daniel/django/site1/media/css/style.css
Now in my html files how do I then reference the CSS file?
As I am in local development, I have done what the Django Docs have send and kept MEDIA_URL as '/static/'.
To then reference the files, do I have done this, but to no avail.
Could someone please point me in the right direction.
Thanks,
Dan
you should be able to include your css like so:
<link href="/static/css/style.css" rel="stylesheet" type="text/css" />
Also, if you're running the django dev server, you have to enable static file serving. Try adding the following to your urls.py:
from django.conf import settings
urlpatterns = patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
Please note that for production use you should disable static file serving within django, and configure your webserver to take over the serving of static content.
Append to urls.py
:
from django.db import settings
# Media (development)
# Serves media content. WARNING!! Only for development uses.
# On production use lighthttpd for media content.
# Set DEBUG to False in production.
if settings.DEBUG:
# Delete the first trailing slash, if any.
if settings.MEDIA_URL.startswith('/'):
media_url = settings.MEDIA_URL[1:]
else:
media_url = settings.MEDIA_URL
# Add the last trailing slash, if have not.
if not media_url.endswith('/'):
media_url = media_url + '/'
urlpatterns += patterns('',
(r'^' + media_url + '(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}
),
)
And then in your template:
<link rel="stylesheet" href="{{ MEDIA_URL }}css/style.css" />
精彩评论