Django CSS background image using STATIC_URL

I just changed my storage server to Amazon S3 and I realized that my background is not loading for my site. I looked and realized that in my CSS (actually SASS) I specified the background URL ( static/mysite/images/background.gif ).

I am wondering how I should fix this problem. Of course, I could just change it to a new static URL, but that seems like bad practice. So I tried loading my background image from {{ STATIC_URL }} into my body HTML, but then it first displays my background before repeat: no-repeat; will display my actual CSS, making the background repeat across the screen while loading CSS. In any case, I do not like to mix my styles in the document.

So what is the best way to provide a relative path for a background image in Django? Should I customize my CSS as a template and direct it through the view? It also seems dirty.

Or should I just forget about making static_url agnostic and just write the code in my CSS?

+6
source share
1 answer

Django compressor!

It allows you to visualize CSS fragments directly inside your template, which are ultimately combined into a single file, or you can even specify options that allow you to parse your CSS files using the django template mechanism and context: http://django_compressor.readthedocs.org/en /latest/settings/#django.conf.settings.COMPRESS_CSS_FILTERS

It's awesome and worth it that you don't need to think about performance when managing CSS.

+8
source

Source: https://habr.com/ru/post/906613/


All Articles