这是我的home.html 我无法在static / images /文件夹中显示图像。 尽管* [09 / Mar / 2020 15:52:09]“ GET /static/images/mona.jpg HTTP / 1.1” 404 1669 *显示在终端中。
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
{% block content %}
{% load static %}
<title>Home</title>
<link rel="stylesheet" href={% static '/css/style.css' %} />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
</head>
<body>
{% include "includes/header.html" %}
<div class="pt-5">
<div class="card d-inline-block">
<img src={% static 'images/mona.jpeg' %} alt="Avatar" style="width:100%;">
<div class="container">
<h4><b>Mona Lisa</b></h4>
<p><a href="#">Architect & Engineer</a></p>
</div>
</div>
<div class="card d-inline-block">
<img src={% static "images/mona.jpg" %} alt="Avatar" style="width:100%">
<div class="container">
<h4><b>The Scream</b></h4>
<p><a href="#">Architect & Engineer</a></p>
</div>
</div>
</div>
{% include "includes/footer.html" %}
{% endblock content %}
</body>
答案 0 :(得分:0)
在源代码周围添加引号,如下所示
{% load static %}
<img src="{% static 'images/mona.jpeg' %}" alt="Avatar" style="width:100%;">
答案 1 :(得分:0)
在生产中,静态文件由Web服务器(Apache,nginx)提供
在开发中,如果您使用django.contrib.staticfiles并且DEBUG设置为True,则将自动提供app_name / static / app_name中的文件。
如果文件是由用户在您的Web应用程序中上传的,则需要按如下所示编辑基本urls.py文件
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
For more info on this see https://docs.djangoproject.com/en/3.0/howto/static-files/
答案 2 :(得分:0)
对不起,这是拼写错误。我真笨。...
在settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'the7thstreet/static'),
'/var/www/static',
]
更正
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
'/var/www/static',
]