簡體   English   中英

樣式表中不支持模板標簽

[英]Template Tags not supported in Stylesheets

假設我在 static 文件的 Django 中創建了一個樣式表。 所以其他一切工作正常。 只是在使用此行時會出錯

.PostProfilePic {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        background-image: url('{{ Profile.ProfilePic.url }}');
        background-size: cover;
        background-position: center;
        position: relative;
        top: 5px;
    }

假設有一個 class 名稱“PostProfilePic”,我想使用數據庫中的圖像作為我的背景圖像,那么我們該怎么做???

錯誤在這一行

background-image: url('{{ Profile.ProfilePic.url }}');

它說'無法解析文件'而且當我在我的 html 頁面中使用這種樣式時,如下所示,它工作得很好

<style>
    .PostProfilePic {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        background-image: url('{{ Profile.ProfilePic.url }}');
        background-size: cover;
        background-position: center;
        position: relative;
        top: 5px;
     }
</style>

在制作單獨的.css 文件時會產生麻煩......對此有任何解決方案嗎?

background-image: url('{{ Profile.ProfilePic.url }}'); 在外部.css文件中將不起作用,因為 django 不會呈現變量。 請注意, url css function 應滿足以下約束

url() CSS function 用於包含文件。 該參數是絕對 URL、相對 URL 或數據 URI。

因此,它不會顯示任何內容,因為此'{{ Profile.ProfilePic.url }}'無法解析任何內容。

花括號{{ - }}只能在模板中呈現變量,因此它在您的第二次嘗試中起作用的原因是它是模板中CSS的內部定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM