[Django]-Django Rest app not using media root correctly to serve images

5👍

The MEDIA_URL setting is the URL path in the browser. The MEDIA_ROOT setting is the root directory on your server, and should be an absolute path.

MEDIA_URL = '/pictures/'

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploaded_pictures')

Also, if you want Product and Store pictures to go into different sub directories, for example pictures/products/ and pictures/store/, you’d need to set the upload_to argument on the model field. For example

picture = models.ImageField(upload_to='products/', ... )

Edit: To serve static and media files during development, add this at the end of the urls.py

if settings.DEBUG:
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    from django.conf.urls.static import static

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

Leave a comment