简体   繁体   English

Django:如何从.css文件链接到静态文件目录?

[英]Django: how to link to static files dir from with .css file?

i am trying to get access to my static images directory (/static/myapp/images/) from inside of a css file. 我试图从css文件内部访问我的静态图像目录(/ static / myapp / images /)。

i initially tried this: 我最初尝试这样做:

{% load staticfiles %}

#header {
    position:relative;
    width: 680px;
    height:99px;
    margin-left:29px;
    margin-right:21px;
    background: url({% static 'myapp/images/header.png' %}) no-repeat;
}

but this did not work 但这没有用

this however works fine: 但是,这很好用:

#header {
    position:relative;
    width: 680px;
    height:99px;
    margin-left:29px;
    margin-right:21px;
    background: url(../images/header.png) no-repeat;
}

does Django template language work inside of css files? Django模板语言可以在CSS文件中使用吗?

or is it just my syntax, and lack of understanding off css! 还是仅仅是我的语法,而对CSS缺乏了解! this css comes from a simple template i downloaded, i no nothing about css, is there another way to link an image here that the template language will work with? 这个css来自我下载的一个简单模板,关于css我什么也没有,还有没有其他方法可以在此处链接图像以使用模板语言?

thanks! 谢谢!

css unless directly on the template does not run through django's template renderer. 除非直接在模板上使用CSS,否则不会通过Django的模板渲染器运行。 You need to use the url, 您需要使用网址,

STATIC_URL = '/static/'

so mysite.com/static/css/mycss.css 所以mysite.com/static/css/mycss.css

or image depending on how your myproject/static looks like (STATICFILES_DIRS / STATIC_ROOT) 或图片,取决于您的myproject / static的外观(STATICFILES_DIRS / STATIC_ROOT)

mysite.com/static/images/myimage.png mysite.com/static/images/myimage.png

CSS files are not processed by the django template system. django模板系统不处理CSS文件。 So your second snippet with relative path is the right way to link to images/fonts. 因此,具有相对路径的第二个片段是链接到图像/字体的正确方法。

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

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