[Answered ]-Setting background image in base template in Django

2đź‘Ť

âś…

Since the background image is a static file, you should use the static url and not media.

{% load staticfiles %}

<style type="text/css">
        body { 
         background: url('{% static "image/containers.jpg" %}') no-repeat center center fixed; 
         -webkit-background-size: cover;
         -moz-background-size: cover;
         -o-background-size: cover;
         background-size: cover;
        }
</style>

You can read more about that in the relevant Django docs: Managing static files.

There is also a good reference on static files in the Django tutorial.

Aside from the HTML generated by the server, web applications generally need to serve additional files — such as images, JavaScript, or CSS — necessary to render the complete web page. In Django, we refer to these files as “static files”.

A bit of extra care is required in order to properly setup static serving, but the documentation is very clear and there are also plenty of relevant questions on SO.

On the other hand, the media url concerns user-uploaded files as you can read in this relevant documentation.

👤Wtower

Leave a comment