pythondjangodeploymentweb-deployment

Retrieve Django media files from render.com disk


I'm deploying my Django application via render.com and I'm using a disk to serve the media files uploaded by the user. I can tell that the media files are being uploaded to the right place by using the render.com shell:

Image files appear in the media folder

However, when I try retrieving the image files in my website template, they don't appear.

Images aren't appearing on the website

My MEDIA_ROOT is "/var/www/[mysite].onrender.com/media/" ([mysite] replaced with the application name) and my MEDIA_URL is '/media/'. On render, the mount path of my disk is '/opt/render/project/src/media/'.


Solution

  • Just got it working, actually! Added this to my urls.py:

    from django.views.static import serve
    from django.urls import re_path
    
    urlpatterns += [
        re_path(r'^media/(?P<path>.*)$', serve, {
            'document_root': settings.MEDIA_ROOT,
        }),
    ]