[英]Django static files working from template but not from CSS file
[英]Django: how to link to static files dir from with .css file?
我試圖從css文件內部訪問我的靜態圖像目錄(/ static / myapp / images /)。
我最初嘗試這樣做:
{% load staticfiles %}
#header {
position:relative;
width: 680px;
height:99px;
margin-left:29px;
margin-right:21px;
background: url({% static 'myapp/images/header.png' %}) no-repeat;
}
但這沒有用
但是,這很好用:
#header {
position:relative;
width: 680px;
height:99px;
margin-left:29px;
margin-right:21px;
background: url(../images/header.png) no-repeat;
}
Django模板語言可以在CSS文件中使用嗎?
還是僅僅是我的語法,而對CSS缺乏了解! 這個css來自我下載的一個簡單模板,關於css我什么也沒有,還有沒有其他方法可以在此處鏈接圖像以使用模板語言?
謝謝!
除非直接在模板上使用CSS,否則不會通過Django的模板渲染器運行。 您需要使用網址,
STATIC_URL = '/static/'
所以mysite.com/static/css/mycss.css
或圖片,取決於您的myproject / static的外觀(STATICFILES_DIRS / STATIC_ROOT)
mysite.com/static/images/myimage.png
django模板系統不處理CSS文件。 因此,具有相對路徑的第二個片段是鏈接到圖像/字體的正確方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.