0

Maybe this question has been asked a lot,
but I still can't understand how to load CSS files when using django... Please can anyone explain to me step by step how to load it?
Can I load the CSS file without the static folder or link, so I don't need to change the urls.conf but just setting in "setting.py" file?

(Sorry if my English is bad") :(

6
  • Similar to: stackoverflow.com/questions/261223/how-do-i-use-css-in-django Commented Nov 10, 2010 at 9:46
  • ya... but i still can't understand... (the static part) Commented Nov 10, 2010 at 9:48
  • I'm not sure whether I've understood your question. You're having trouble setting up the media folder? Commented Nov 10, 2010 at 9:50
  • I don't know Django, but I think the OP is asking for a step-by-step guide on including a CSS file into his website. Commented Nov 10, 2010 at 9:58
  • yah... mybe it's becaus of my english... sorry... i just want to load css, without using "staticfiles" Commented Nov 10, 2010 at 10:10

2 Answers 2

2

on local machine: you have to add:

(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/media'}),

on a server you don't (and more you shouldn't for security reasons) need the previous line.

So finally: here is my architecture:

project/
   app1/
     __init.py__
     views.py
   public/
     site_media/
        js/
           example.js
        css/
           example.css

in my settings.py:

MEDIA_ROOT = '/thecompletepath/public/site_media/'
MEDIA_URL = '/site_media/'

and in my templates, i use:

<link rel="stylesheet" type="text/css" href="/site_media/css/example.css" media="screen" />
Sign up to request clarification or add additional context in comments.

1 Comment

Use RequestContext with your views and use {{ MEDIA_URL }} in place of /site_media/, and you're golden. You should also mention that by "on a server you don't ... need the previous line", you meant that one should use server-specific configuration to serve the media through the server rather than through django.
2

Just add a normal link tag to your template.

<link rel="stylesheet" type="text/css" href="http://example.com/path/to.file.css">

Unless you have a dynamic CSS file, you would not want to have it related to anything in urls.py. Likewise, if you're not using the media (static) folder, you don't need to change anything in settings.py. Just insert it into your HTML in the template.

If you don't want to use an external CSS file, of course, you can always just put <style> tags into your templates.

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.