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.

4
  • is "django.core.context_processors.media" in TEMPLATE_CONTEXT_PROCESSORS? Commented Jul 30, 2013 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. Commented Jul 30, 2013 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 Commented Jul 30, 2013 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) Commented Jul 30, 2013 at 22:19

1 Answer 1

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.

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

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.