1

I can load CSS files, which is stored in static folder. However, when I try to set background image in the CSS file. I failed.

I used URL({{ STATIC_URL }}images/img01.jpg) in css, but it didn't load the image.

paul marcus
  • 43
  • 1
  • 5
  • is `"django.core.context_processors.media"` in `TEMPLATE_CONTEXT_PROCESSORS`? – Daniel Rosenthal Jul 30 '13 at 20:00
  • 1
    @daniel, I think he means he uses the "STATIC_URL" context variable **inside** the css. To paul: if that's the case, in your css use relative paths. eg url(../images/img01.jpg) or url(images/img01.jpg) depending on the location of your css. Static files are not processed by the template rendering engine, therefore you cannot use context variables. – ppetrid Jul 30 '13 at 20:35
  • @ppetrid solved. thanks! btw, is there any difference of using js and css in html? My css works but js doesn't – paul marcus Jul 30 '13 at 22:14
  • You mean inline css and inline js? Both should work! You might want to check for js syntax errors also, just in case. (I will make a proper answer of my previous comment, if you would be so kind to accept it :P) – ppetrid Jul 30 '13 at 22:19

1 Answers1

0

You should always use relative paths in your css. Eg. url(../images/img01.jpg) or url(images/img01.jpg) depending on the location of your css file.

Static files are not processed by the template rendering engine, therefore you cannot use context variables or any other django templating syntactic elements. You should think your static files as files being served "as is", with no intermediate python or other processing.

ppetrid
  • 3,745
  • 27
  • 29