Django静态文件图像导入模板404错误

问题描述

我正在尝试将图片加载到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

-> static / dribblz / images / image.png

-> templates / home.html

我不知道我还能提供什么其他有用的信息。 预先感谢。

解决方法

static已经在/static之前加上了生成的网址,

因此您无需再次添加它:{% static 'dribblz/images/multicolored-soccer-ball-on-green-field-47730.png' %}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...