Django 不显示静态图像,但通过在控制台窗口中显示 ["GET /static/image/down.jpg HTTP/1.1" 404 1771] 来显示其替代文本alt

问题描述

我只是想在模板中显示我的静态图像。 我已经寻找了解决此问题的所有可能方法并尝试了各种方法,但无法在模板中获取我的静态图像。显示了相应图像的替代文本,但图像本身未加载并在终端窗口中获​​取此内容

"GET /static/image/down.jpg HTTP/1.1" 404 1771

下面给出了我的 Django 项目的布局方式 project files hierarchy

在 urls.py 中

from django.conf.urls import include,url
from django.contrib import admin
from django.urls import path
from myapp import views

from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
.......]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,document_root= settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)

在 settings.py 中

STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR,'/static/')

MEDIA_ROOT = os.path.join(BASE_DIR,"media/")
MEDIA_URL= "/media/"

在模板 question.html 中

我尝试了不同的方法来访问图像[在 html 页面顶部使用 {% load static %} 标签] 喜欢

<img src="{% static 'image/down.jpg' %}">   
<img style="width: 245px; height: 247px;"
                                    alt="Image description"
                                    src="{% static 'image/down.jpg' %}"/>
<img src="/static/image/up.jpg" alt="Second-image" />

但无法解决问题

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)