在CSS文件中使用Django变量 [英] Using a Django variable in a CSS file
问题描述
我尝试使用Django模板引擎或任何其他方式创建动态 CSS文件。
规则如下:
background-image:url('http://static.example.com/example.png ');
其中 http://static.example.com
对应于Python中的 STATIC_URL
变量。使用Django模板引擎,我理论上可以这样写:
background-image:url('{{STATIC_URL}} example.png');我的问题是,我如何使用Django模板引擎(或任何其他方式)生成CSS?动态?解决方案您基本上有两个选项:
-
使用urls.py中的条目动态地提供CSS,就像它是一个HTML页面一样。您的模板文件只是CSS而不是HTML,但会使用正常的Django模板语法等。
-
快捷方式:使用相对路径引用您的背景图片。这对于您的环境可能是可能的,也可能不是可能的,但是它是一个方便的方式使静态CSS文件根据托管的位置引用不同的路径。
I am trying to create a dynamic CSS file using the Django templating engine or any other means.
Currently, I have a CSS rule that looks like this:
background-image: url('http://static.example.com/example.png');
Where http://static.example.com
corresponds to the STATIC_URL
variable in Python. Using the Django templating engine, I could theoretically write something like this:
background-image: url('{{ STATIC_URL }}example.png');
My question is, how can I use the Django templating engine (or any other means) to generate CSS dynamically?
解决方案 You basically have two options:
Serve your CSS dynamically, with an entry in urls.py, etc., just as if it were an HTML page. Your template file will simply be CSS instead of HTML, but will use normal Django template syntax, etc.
Shortcut: Reference your background image with a relative path. This may or may not be possible for your environment, but it's a convenient way of having a static CSS file reference different paths depending on where it's hosted.
这篇关于在CSS文件中使用Django变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!