Search code examples
pythondjangoruntime-errortypeerror

Django:MEDIA_ROOT error : _getfullpathname: path should be string, bytes or os.PathLike, not tuple


I am new to Django Framework. I am trying to upload an image to the customer table and this error occurs:

_getfullpathname: path should be string, bytes or os.PathLike

I am using this code to upload:

MEDIA_ROOT = [BASE_DIR, 'static/images']

I changed the code to

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

but it says NameError: name 'os' not defined. What's the reason for the errors and how solve them?

settings.py

STATIC_URL = '/static/'
MEDIA = '/images/'
STATICFILES_DIRS = [BASE_DIR, 'static']
MEDIA_ROOT = [BASE_DIR, 'static/images']

models.py

class Customer(models.Model):
    user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
    name = models.CharField(max_length=200, null=True)
    phone = models.CharField(max_length=11, null=True)
    email = models.EmailField(max_length=200, null=True)
    profile_pic = models.ImageField(null=True, blank=True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)

Solution

  • I had same problem, this probably works change it to this :

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

    and for the error os not defined you just have to import it

    import os
    from pathlib import Path