简体   繁体   English

如何在 css 文件中包含背景图片以供 django 使用?

[英]How do I include a Background picture in a css file for django usage?

I'm trying to include a background picture into one of my css files which is then loaded into a html file using djangos compressor module.我正在尝试将背景图片包含到我的 css 文件之一中,然后使用 djangos 压缩器模块将其加载到 html 文件中。 The project looks as follows:该项目如下所示:

djangoproject
|-myproject (with setup.py etc)
|-statics
  |-images
  |-css
|-templates
  |-index.html

In the index.html i used在索引中我使用了 html

{% load static %}
{% load compress %}

{% compress css %}
    <link rel="stylesheet" href="/static/css/style.css">
{% endcompress %}

and in this style.css, I would like to include a background picture from statics/images/bg.jpg .在这种 style.css 中,我想包含一张来自statics/images/bg.jpg的背景图片。 I tried to do this as follows:我试图这样做如下:

background: url('../images/bg.jpg') 50% 0 repeat-y fixed;

but the picture wont appear.但图片不会出现。 So does anyone know how to refere the url here?那么有人知道如何在这里引用 url 吗? Because outside of django, this way works well.因为在 django 之外,这种方式效果很好。

Thanks for any advie and sorry for my bad english skills (just ask if you are not able to understand something because of bad word choice etc.)!感谢您的任何建议,并为我糟糕的英语技能感到抱歉(请问您是否因为错误的单词选择等而无法理解某些内容)!

Thanks!谢谢!

Assuming you have set STATIC_URL='/static/' in your settings and included directory containing images/ directory in STATICFILES_DIRS , you can write your css as follows:假设您在设置中设置STATIC_URL='/static/'并在STATICFILES_DIRS中包含了包含images/目录的目录,您可以按如下方式编写 css:

background: url('/static/images/bg.jpg') 50% 0 repeat-y fixed;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM