views:

11991

answers:

5

I am creating my application using Django, and am wondering how I can make Django use my CSS file? What settings do I need to do to make Django see the css file?

NB: On a local machine

+9  A: 

What settings do i need to do to make Django see the css file?

None.

Make sure your template includes the CSS file (as standard HTML does) and put the CSS file on the media server.

To clarify: With Django it is highly recommended that you serve all your media (everything that isn't dynamic html) from a different server instance. How you implement that is completely up to you but most people create a subdomain.

Oli
+8  A: 

More generally stated, you're asking how to serve a static file from Django. If you are running under Apache, you should read http://docs.djangoproject.com/en/dev/howto/deployment/modpython/

If you are running the development server (say, on your laptop), read http://docs.djangoproject.com/en/dev/howto/static-files/

Do note the big, fat disclaimer:

  • Using this method is inefficient and insecure.
  • Do not use this in a production setting.
  • Use this only for development.
Peter Rowell
A: 

I'm still unsure how to reference the media files from a template.

I have media_root set to d:\media and media_url set to /media/. I tried link="/media/styles.css" and a few other variations in the template but couldn't get it to work. Any ideas?

Josh Smeaton
+4  A: 

If your using the development server follow this: http://docs.djangoproject.com/en/dev/howto/static-files/ to setup your URL's, then reference you media files in the template...say an image inside an image folder from: "/site_media/images/foo.gif"

Joe
A: 

Well the easiest way to use css with django, is to add it to your templates as static-files.

But it's a bit like ajax, I didn't find anything that tells how to include it in a standard way.

There is a css-compressor module for django if you want to optimise its size.

mnml