我想用一个图像文件作为Django的背景图像.但我不知道是怎么回事. 首先,我读取了this,并try 在CSS文件中编写如下内容.

#third{
    background: url("img/sample.jpeg") 50% 0 no-repeat fixed;
    color: white;
    height: 650px;
    padding: 100px 0 0 0;   
}

但这是行不通的.

{% load staticfiles %}
#third{
    background: url({% static "img/sample.jpeg" %}) 50% 0 no-repeat fixed;
}

#third{
    background: url("../img/sample.jpeg") 50% 0 no-repeat fixed;
}

也不要工作.

当你在CSS文件上使用背景图片时,你通常怎么写CSS文件?你能给我一些建议吗?

C:\~~~~~~> dir hello\static\img
2016/09/28  19:56             2,123 logo.png
2016/09/24  14:53           104,825 sample.jpeg


C:\~~~~~~> dir hello\static\css
2016/09/29  20:27             1,577 site.css


C:\~~~~~~> more lemon\settings.py
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)


C:\~~~~~~> more hello\templates\base.html
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static "css/site.css" %}" />

Django版本:1.9.2

推荐答案

使用以下命令:

    #third{
     background: url("/static/img/sample.jpeg") 50% 0 no-repeat fixed;
     color: white;
     height: 650px;
     padding: 100px 0 0 0;   
     }

这很可能会起作用.在图片地址前使用"/静电/",然后try 使用它们.谢谢

Django相关问答推荐

django命令中的no_color不起作用

Django-将html数据保存到quillfield中

Django ORM Groupby

如何在Django表单中传递self.请求数据?

Django中每个类型/代理的最新行

通过 OrderingFilter 过滤平均和

Django prefetch_related 与 3 个不直接相关的模型

根据当前对象中的多对多字段过滤对象

为什么 timezone.now 在作为默认值应用于 Django 中的 DateField 时显示future 日期

如何在 createsuperuser 中实例化表

如何在Django中制作一个不 Select 以前日期的日期 Select 器

在 Django Admin change_list 视图中更改 list_editable 字段时保存 Django 模型

QuerySet对象在bulk_update中没有属性pk

如何在 Django 中将下一个 8 月 1 日添加为 DateTimeField 默认值

Django 身份验证系统修改

Django 嵌套事务 - with transaction.atomic()

Python / Django 中的 Unicode 与 UTF-8 混淆?

如何在django中生成临时文件然后销毁

如何在 Django 中向 ChoiceField 添加class?

django-rest-framework 接受 JSON 数据?