【问题标题】:Django Background Img CSSDjango 背景图片 CSS
【发布时间】:2018-09-29 07:16:27
【问题描述】:

我的背景图片没有通过我的 CSS 文件加载。它通过前端加载:

<img class="top" src={% static "/store/img/store.jpg" %}>

在 div 内部但不包含: background: url('{{ STATIC_URL }}/store/img/store.jpg');在 css 文件上。

我的路径是apps/store/static/store/img/store.jpg,这样很好。

HTML:

<div class="w3-display-container w3-content w3-wide" style="max-width:1600px;min-width:500px" id="home">
  <div class="w3-display-bottomleft w3-padding-large w3-opacity">
    <h1>SAN MIGUEL'S EXPORTA</h1>
  </div>
</div>

CSS:

#home{
    /* The image used */
background:  url('{{ STATIC_URL }}/store/img/store.jpg') no-repeat;

/* Full height */
height: 100%;

/* Create the parallax scrolling effect */
background-attachment: fixed;
background-position: center;
background-size: cover;

}

【问题讨论】:

    标签: python css django background-image


    【解决方案1】:

    如果您使用 django 的标准模板引擎,您需要在引用静态文件之前加载它们。在&lt;img class="top" src={% static "/store/img/store.jpg" %}&gt;之前粘贴{% load static %}

    这里有更多关于静态文件的帮助: https://docs.djangoproject.com/pt-br/2.0/howto/static-files/#configuring-static-files

    如果您愿意,请随时向我提出更多问题!

    【讨论】:

    • {% load static %} 在那里。我能够通过 HTML 但不能通过 CSS 文件加载图像。我如何加载它? #home{ background-image: url('store/img/store.jpg') 无重复; .....这不起作用
    • 我认为通过 CSS 文件是不可能的,为什么不直接在 HTML 中做呢?试试background: {% static "/store/img/store.jpg" %} no-repeat;