我正在学习Django,并建立了第一个测试投票应用程序,但遇到了背景图像的问题。绿色文本可以工作,但添加背景图像不起任何作用(背景仅为白色)。你知道为什么它不能正常工作吗?
背景图片C:\Users\xxx\Python\Django\mysite\polls\static\polls\images\sample123.jpg
样式CSS: C:\Users\xxx\Python\Django\mysite\polls\static\polls\style.css
li a {
color: blue;
}
body {
background: url('C:\Users\xxx\Python\Django\mysite\polls\static\polls\images\sample123.jpg');
background-repeat: no-repeat;
}索引HTML: C:\Users\xxxx\Python\Django\mysite\polls\templates\polls
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}发布于 2020-12-22 11:54:59
这应该能起到作用
body {
background: url('/static/images/sample123.jpg');
background-repeat: no-repeat;
}发布于 2020-12-22 17:48:09
您的背景图像没有渲染,因为对于背景图像,您提供的是相对路径。提供绝对路径或使用静态模板标记。
body {
background: white url({%static 'polls\images\sample123.jpg'%}) no-repeat;
}发布于 2020-12-29 07:42:32
尝试将此代码添加到polls/urls.py文件中
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
#....
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) #with out this, the images will not be displayed在您的polls/settings.py文件中
STATIC_URL = '/static/'
MEDIA_URL = '/static/images/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images/')https://stackoverflow.com/questions/65402732
复制相似问题