0
votes

I am having images in my media folder and I want to display them but Django is not displaying images other than static folder. In my setting

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

In my urls.py after url pattern I added this

+ static(MEDIA_URL, document_root=MEDIA_ROOT)

In my templates

<img class="card-img-top" style="" src="{{result.object.thumbnail.url}}" alt=""></a>               
   <p>{{result.object.thumbnail.url}}</p>

It is showing the correct path but not showing the image, I am unable to figure out the problem. Thanks

3
is MEDIA folder placed in correct place in your project? is path valid?Chiefir
Sir media folder is placed in BASE_DIR, yes the path is correct!Pankaj Sharma
check my answer, in case you have media forder in the root of your projectLemayzeur

3 Answers

2
votes

Your MEDIA_ROOT have the path of the root of your project. You must join to it, the direcotry of your media. (I suppose media/ is the directory name where you upload all your media) I think you should have your MEDIA_ROOT like that.

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

MEDIA_ROOT have root path of your project. MEDIA_URL must be correct. Check following setting.py of your project

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

urls.py of project

 urlpatterns = [
                  url(r'^admin/', admin.site.urls),
                  # url('', include('app1.urls')),
                  url('home/', myapp_views.index)
              ] + static(settings.MEDIA_URL, 
             document_root=settings.MEDIA_ROOT)

I was facing same error from the last 5 days , follow django documentation, many tutorials but nothing work for me but finally I found the solution

I am showing the index.html on home means I have not given any endpoint in url of my app and project url file

app/urls.py

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

project/urls.py

urlpatterns = [
                  url(r'^admin/', admin.site.urls),
                  # url('', include('app1.urls')),
                  url('', myapp_views.index)
              ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Rest of code remains the same and I changed the following in both files

app/urls.py

    url('home/', 'views.index', name='index page'),

project/urls.py

 url('home/', myapp_views.index)

That's work for me. Accept the answer if also work for you

0
votes

This is a chunk of my HTTP logs from django v3.2 running wagtail. Looks as though django is trying to tell me where the missing media is but not able to. Because this is a thicket page with gallery sub-images of "featured pages" the view source in my browser doesn't reveal the image file attempt, but I am assuming it is the same issue as OP's with misconfigured MEDIA_ROOT. Strangely, not seeing any errors in the page's linked images when I bring the child page up in wagtail admin. Anyone have an idea why the missing image won't bubble up to the HTTP logs, or what causes the "media/not-found" substitution for the "real" item causing the 404?

[25/Aug/2021 20:28:53] "GET /static/wagtailadmin/images/bg-dark-diag.svg HTTP/1.0" 200 700

Not Found: /media/not-found [25/Aug/2021 20:29:18] "GET /media/not-found HTTP/1.0" 404 3252

Not Found: /media/not-found [25/Aug/2021 20:29:18] "GET /media/not-found HTTP/1.0" 404 3252