I am facing a situation where I need to display an image as the background of a div only if it is available. If the image is not present, I do not want the div to be visible at all. I am working with django templates and here is my current approach:
{% if post.image %}
<div class="background"></div>
{% endif %}
Here is the CSS code for the div:
.background{
position: absolute;
z-index: -1;
background-image: url("{{ post.image.url }}");
width: 100%;
height: 400px;
}
The images are stored in AWS S3. The issue I am facing is that when there is no image associated with the object, the page fails to load and throws an error stating that the 'image' attribute does not have a file associated with it. How can I resolve this issue?