0

Media files are not loading properly when pulling from database. Although, when inspecting page, source seems to be correct.

I have preformed migrations, collectstatic. static files are loaded to the html file

settings.py

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

MEDIA_URL = '/media/'

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

urls.py

 urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', cars.views.index_page, name='index_page'),]

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

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

models.py

nuotrauka = models.ImageField(default='default.jpg', blank=True)

index.html

 {% for detail in carDetails %}

     <img src="{{detail.nuotrauka.url}}" alt="detail-image">

 {% endfor %}

Image is simple not loading. It seems like image src is not able to locate the image or media folder is not being recognized at BASE_DIR. I might be just simple missing something but at this point I am not able to see it.

I do think it has something todo with folder structure and maybe even BASE_DIR. Because URL seem to be fine, but the file is not found

Dom
  • 1
  • 2

1 Answers1

0

In :

urlpatterns += static(settings.STATIC_URL, documnet_root = settings.STATIC_ROOT)

You wrote (document) wrong.

Cheers :)

mng back
  • 40
  • 1
  • 10
  • Nice catch but this did not fix the problem. I do think it has something todo with folder structure and maybe even BASE_DIR. Because URL seem to be fine, but the file is not found – Dom Jan 05 '19 at 09:11
  • Did u fix it in documnet_root = settings.MEDIA_ROOT too ? – mng back Jan 05 '19 at 09:13
  • Yeah, I did. It is still not working. I do see this when inspecting page ` ` – Dom Jan 05 '19 at 09:54