样式表中不支持模板标记

2024-06-16 14:47:18 发布

您现在位置:Python中文网/ 问答频道 /正文

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

上面写着cannot resolve file

而且,当我在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文件时会产生麻烦…有什么解决办法吗


Tags: 文件imageurlsize错误positioncoverprofile
1条回答
网友
1楼 · 发布于 2024-06-16 14:47:18

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

The url() CSS function is used to include a file. The parameter is an absolute URL, a relative URL, or a data URI.

因此,它不会显示任何内容,因为这个'{{ Profile.ProfilePic.url }}'不会解析为任何内容

大括号{{-}}只能在模板中呈现变量,这就是为什么它在第二次尝试中可以工作的原因,因为它是模板中CSS的内部定义

相关问题 更多 >