As mentioned above, “background.png” image is not shown in the poll app I am creating following the “Writing your first Django app” tutorial.
This is the code in polls/templates/polls/index.html
{% load static %}
<!doctype html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<title>My test page</title>
<link rel="stylesheet" href="{% static "polls/style.css" %}">
</head>
<body>
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url "polls:detail" question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
</body>
</html>
This is the code in polls/static/polls/style.css
body {
background: white url("images/background.png") no-repeat;
}
li a {
color: green;
}
You’ve used a relative URL in the CSS’s url() function, and this is always relative to the location of the stylesheet.
So, if your stylesheet is at the URL /static/polls/style.css it’s looking for an image at /static/polls/images/background.png. I assume there isn’t one at that location?
It’s usually safest to use an absolute URL (starting with a /).
So if your image is at the URL /static/images/background.png (I’m guessing, it might not be) you’d use:
body {
background: white url("/static/images/background.png") no-repeat;
}
Thanks for the response. I don’t know but it works miraculously today without changing yesterday’s code. And my image is at djangotutorial\polls\static\polls\images\background.png and css is at djangotutorial\polls\static\polls\style.css
The CSS url() function uses the absolute or relative web address of the image. It doesn’t matter where on the filesystem the image is.