0

I want to use a background image in one of my web pages served up by Django. Ideally, I use a css file and use css background-image property, but when I put it in my css file, it will not render, but it renders if I put the code in the HTML document (internal style sheet). So, to sum it up. the same exact code, here:

<style>
    body {
        background-image: url("{% static 'Kitchen.jpg' %}"); 
background-repeat: no-repeat; background-attachment: fixed;
    }
    </style>

The above code works in the html doc but not in the css (minus the html "style" tags). Furthermore, other code in the same css file works just fine. For some reason, the background-image property will not execute in the css file.

Unless someone has another idea, seems like this is a Django software bug.

BILL WAGNER
  • 155
  • 3
  • 13

2 Answers2

0

Are you using background-image: url("{% static 'Kitchen.jpg' %}"); in the css file when you're trying to import into the html page? if so you should change it to background-image: url("<path to file>\kitchen.jpg");

Luke Kavanagh
  • 229
  • 2
  • 5
  • 13
  • Hi Luke, I have also tried what you are suggesting, to no avail. Technically speaking, this: url("{% static 'Kitchen.jpg' %}"); should work. Something is amiss in the static file system it seems that is causing this to not work. – BILL WAGNER Apr 22 '16 at 21:24
0

It's Working for me.

You forget to write

{% load staticfiles %}

after HTML Tag

<html>
{% load staticfiles %}
<style>
    .gd{
        background: url("{% static 'images/avatar.jpg' %}");
    }
</style>
<body>
<p class="gd"></p>
</body>
</html>
Neeraj Kumar
  • 3,851
  • 2
  • 19
  • 41
  • My html doc has {% load staticfiles %} in it. This, above, works, as I stated in my original post. What does NOT work is in the style sheet (see original post). – BILL WAGNER Apr 22 '16 at 20:54