0

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 folder 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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I'm afraid it doesn't work with that, and I tried before with
<link href="{{STATIC_URL}}/style.css" rel="stylesheet" type="text/css" /> too.
are you using the django dev server? If so have you enabled static file serving?
Yes, I am. But these are automatically enabled unless you choose to use another server for local development.
"also, if you're running the django dev server, you have to enable static file serving. Try adding the following to your urls.py: (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),"
|
0

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" /> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.