【问题标题】:Django static file image into template 404 errorDjango 静态文件图像转换为模板 404 错误
【发布时间】:2020-12-19 02:40:31
【问题描述】:

我正在尝试将图片加载到 Django 3.1 模板中。我遵循https://docs.djangoproject.com/en/3.0/howto/static-files/ 上的文档并在其他地方寻找答案,但无法解决问题。

以下是我认为相关的所有代码:

来自settings.py

INSTALLED_APPS = [
'memberships.apps.MembershipsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]


STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/var/www/static/',
]

来自 url.py

urlpatterns = [
path('', views.index, name='index'),
path('memberships/', include('memberships.urls')),
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

从模板文件中:

{% load static %}
...
<img src="{% static 'static/dribblz/images/multicolored-soccer-ball-on-green-field-47730.png' %}" alt="Football on the pitch">

我的文件结构如下:

dribblz/

->dribblz/

--> settings.py

--> url.py

-->views.py

->静态/dribblz/images/image.png

->模板/home.html

我不知道我还能提供哪些其他有用的信息。 提前致谢。

【问题讨论】:

    标签: python-3.x django django-staticfiles


    【解决方案1】:

    static 已经将 /static 添加到生成的 url,

    所以你不需要再次添加它:{% static 'dribblz/images/multicolored-soccer-ball-on-green-field-47730.png' %}

    【讨论】:

      猜你喜欢
      • 2018-05-24
      • 2011-12-18
      • 2021-02-28
      • 2016-04-08
      • 2019-01-23
      • 2021-11-25
      • 2018-07-21
      • 2020-05-07
      • 1970-01-01
      相关资源
      最近更新 更多