0

I`ve created a new Django project and ran into a problem with static images. So, apparently when i was connecting my css and javascript files with static it worked well, but when i tried to connect images they won't load. here is my settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_DIRS = ['static']

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

urls.py

from django.contrib import admin
from django.urls import path, include

from store import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('mstore.urls')),
]


if settings.DEBUG:
    from django.conf.urls.static import static

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

here is example of how i display it in template

<a href="#" class="item__image">
<picture><source srcset="img/collections/featured/02.webp" type="image/webp">
<img src="{% static 'mstore/img/collections/featured/02.png' %}" alt="Lore Walnut"></picture></a>

Please, before answering note that i checked the src of the image in browser, and it worked.

src of image in browser i copied this link and pasted in browser and got this enter image description here Besides, css and js works great, so i think the problem is in images

I tried to run python manage.py collectstatic and it worked perfectly.

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
Librin
  • 3
  • 3
  • In urlpatterns you need another static() pattern with STATIC_ROOT and STATIC_URL. CSS and JS are most likely cached and without browser cache would break as well. – Ivan Starostin Aug 05 '23 at 20:56
  • 1
    Your `srcset` has an URL that is _not_ `{% static %}`'d. – AKX Aug 05 '23 at 21:02
  • 1
    check your static directory permission. This is a duplicate question. check this link. https://stackoverflow.com/questions/51690071/django-media-not-loading-static-files-working – Milad Gialni Aug 05 '23 at 18:45
  • What is static directory permission? i cant understand what to do, i changed static path to absolute, run pycharm as administrator but still no changes – Librin Aug 05 '23 at 20:09

1 Answers1

0

So I just forgot to static my srcsets and it caused the problem.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Librin
  • 3
  • 3