如何使用 Django 从元素的 CSS 调用静态图像?
How do I call a static image from the CSS of an element with Django?
我使用 bootstrap studio 制作了一个简单的前端页面,我正在学习 django,所以我正在为应用程序使用 bootstrap 前端。
所有静态资源都在我调用的静态文件夹中:
{% load static %}
图像位于:static 'img/map.png'
在下面的代码中,div 使用 css 调用背景图像 - background:url('assets/img/map.png')
但这当然行不通,因为我有一个名为 static 的文件夹,其中包含静态资产
<div class="col" style="text-align: center;display: flex;justify-content: center;align-items: center;flex-flow: column;min-width: 300px;background: url('assets/img/map.png');background-size: cover;">
<div style="background: rgba(255,255,255,0.85);margin-top: 66px;margin-bottom: 66px;">
<h1>Diverse Locations</h1>
</div>
</div>
有什么方法可以让 div 指向正确的背景图片吗?
注意:我通过将静态内容复制到项目文件夹中创建了静态文件夹,定义了 STATIC_ROOT 和 STATICFILES_DIRS 变量以及 运行 命令:python manage.py 收集静态数据。
使用static
模板标签:background: url('{% static 'img/map.png' %}');
根据@IainShelvington 的建议
我使用 bootstrap studio 制作了一个简单的前端页面,我正在学习 django,所以我正在为应用程序使用 bootstrap 前端。
所有静态资源都在我调用的静态文件夹中:
{% load static %}
图像位于:static 'img/map.png'
在下面的代码中,div 使用 css 调用背景图像 - background:url('assets/img/map.png')
但这当然行不通,因为我有一个名为 static 的文件夹,其中包含静态资产
<div class="col" style="text-align: center;display: flex;justify-content: center;align-items: center;flex-flow: column;min-width: 300px;background: url('assets/img/map.png');background-size: cover;">
<div style="background: rgba(255,255,255,0.85);margin-top: 66px;margin-bottom: 66px;">
<h1>Diverse Locations</h1>
</div>
</div>
有什么方法可以让 div 指向正确的背景图片吗?
注意:我通过将静态内容复制到项目文件夹中创建了静态文件夹,定义了 STATIC_ROOT 和 STATICFILES_DIRS 变量以及 运行 命令:python manage.py 收集静态数据。
使用static
模板标签:background: url('{% static 'img/map.png' %}');
根据@IainShelvington 的建议