Python 如何在css文件中包含背景图片以供django使用?
我试图将背景图片包含到我的css文件中,然后使用djangos压缩模块将其加载到html文件中。该项目如下所示:Python 如何在css文件中包含背景图片以供django使用?,python,css,django,Python,Css,Django,我试图将背景图片包含到我的css文件中,然后使用djangos压缩模块将其加载到html文件中。该项目如下所示: djangoproject |-myproject (with setup.py etc) |-statics |-images |-css |-templates |-index.html background: url('../images/bg.jpg') 50% 0 repeat-y fixed; background: url('/static/images
djangoproject
|-myproject (with setup.py etc)
|-statics
|-images
|-css
|-templates
|-index.html
background: url('../images/bg.jpg') 50% 0 repeat-y fixed;
background: url('/static/images/bg.jpg') 50% 0 repeat-y fixed;
在index.html中,我使用了
{% load static %}
{% load compress %}
{% compress css %}
<link rel="stylesheet" href="/static/css/style.css">
{% endcompress %}
但这幅画不会出现。那么有人知道如何在这里引用url吗?因为在django之外,这种方法很有效
谢谢你的建议,也为我糟糕的英语技能感到抱歉(如果你因为选词不当而不能理解某些东西,请尽管问吧!)
谢谢 假设您在设置中设置了
STATIC\u URL='/STATIC/'
,并在STATICFILES\u DIRS
中包含了包含images/
目录的目录,您可以按如下方式编写css:
djangoproject
|-myproject (with setup.py etc)
|-statics
|-images
|-css
|-templates
|-index.html
background: url('../images/bg.jpg') 50% 0 repeat-y fixed;
background: url('/static/images/bg.jpg') 50% 0 repeat-y fixed;
你的css来自/static/但是你的图片来自statics/是打字错误吗?是的,我设置了
static\u URL='/static/'
。我包括了STATICFILES\u DIRS=[os.path.join(BASE\u DIR,'statics'),]
。由于图像目录位于静态目录(/static/images/
)中,因此您的解决方案应在不包含图像/
的情况下工作,不是吗?排序答案否。请阅读有关静态文件如何工作的更多信息