Search code examples
python-3.xdjangodjango-staticfiles

Django static file image into template 404 error


I am trying to load a picture into a a Django 3.1 Template. I followed the documentation on the https://docs.djangoproject.com/en/3.0/howto/static-files/ and looked elsewhere for an answer but was not able to solve the issue.

Here is all the code that I think is relevant:

from 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/',
]

from 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)

and from the template file:

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

My files are structured such as:

dribblz/

-> dribblz/

--> settings.py

--> url.py

--> views.py

->static/dribblz/images/image.png

->templates/home.html

I don't know what other information I can give that may be useful. Thanks in advance.


Solution

  • static already prepend /static to the generated url,

    so you don't need to add it again : {% static 'dribblz/images/multicolored-soccer-ball-on-green-field-47730.png' %}