样式表不支持模板标签

时间:2020-07-06 06:36:32

标签: python html css django django-templates

假设我在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;
    }

假设有一个类名'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文件时会造成麻烦...对此有任何解决方案?

1 个答案:

答案 0 :(得分:1)

外部background-image: url('{{ Profile.ProfilePic.url }}');文件中的此.css将不起作用,因为django无法呈现变量。请注意,url CSS函数应满足以下约束条件

url()CSS函数用于包含文件。该参数是绝对URL,相对URL或数据URI。

因此,由于此'{{ Profile.ProfilePic.url }}'不能解析为任何内容,因此不会显示任何内容。

大括号{{-}}只能在模板中呈现变量,因此之所以在第二次尝试中起作用是因为它是模板中CSS的内部定义。 / p>