[Django]-Can't upload image in django use MEDIA_ROOT and MEDIA_URL

13👍

MEDIA_ROOT is an absolute path to the uploaded images so you should change the setting to something like this:

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images/upload')

The second problem is in the image field definition. upload_to argument is a path relative to the MEDIA_ROOT/MEDIA_URL.

image = models.ImageField(upload_to='product')

And it is better to add some strftime() formatting to reduce the number of files in the single directory:

image = models.ImageField(upload_to='product/%Y/%m/%d')

Leave a comment