1

http://127.0.0.1:8000/static/apis/icons/random-svgrepo-com.svg gives me error 404 not found.

settings.py

STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
STATICFILES_DIR = [
    os.path.join(BASE_DIR, 'staticfiles/'),
]
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')

template.html

{% load static %}
<img src="{% static 'apis/icons/random-svg.svg' %}">

root urls.py

from django.conf.urls.static import static
from django.conf import settings

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Work directory:

I am running ./manage.py collectstatic, and then run the server. But it doesn't show me my image.

My questions are:

  1. Difference between STATICFILES_DIR and STATICFILES_DIRS
  2. What I am doing wrong? Why my static files doesn't appear to work fine?

Thanks!

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
Docker
  • 159
  • 1
  • 2
  • 10
  • `in production on local` what do you mean by this? `in production` = `debug = false`? _in production_ usually means somewhere else than _local_, on some remote server. – Ivan Starostin Sep 25 '22 at 15:54
  • 1
    question 1 - there is only one thing https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-STATICFILES_DIRS – Ivan Starostin Sep 25 '22 at 15:56
  • @IvanStarostin, DEBUG is set to False, but its on my local computer. – Docker Sep 26 '22 at 07:44
  • Yeah, totally :( But not the accepted answer, but what is under it. I changed my root urls.py to ```urlpatterns += [ re_path(r'^media/(?P.*)$', serve,{'document_root': settings.MEDIA_ROOT}), re_path(r'^static/(?P.*)$', serve,{'document_root': settings.STATIC_ROOT}) ] ```. I ll post an answer – Docker Sep 26 '22 at 08:12

1 Answers1

1

Changed my root urls.py to:

from django.conf import settings
from django.views.static import serve

urlpatterns += [
    re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
    re_path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT})
]
Docker
  • 159
  • 1
  • 2
  • 10