Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errno - 13 Permission denied: '/media/ - Django

I am using Django 3.1 in ubuntu,

I got an error while uploading media files

PermissionError at /admin/main/artist/1/change/
[Errno 13] Permission denied: '/media/artists'

Exception Type: PermissionError
Exception Value:    
[Errno 13] Permission denied: '/media/artists'
Exception Location: /usr/lib/python3.8/os.py, line 223, in makedirs
Python Executable:  /home/rahul/.local/share/virtualenvs/music-69qL54Ia/bin/python

This code works in windows, but not in ubuntu

Settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static']

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / '/media/'

Models.py

class Artist(models.Model):
    image = models.ImageField(upload_to='artists/%Y/%m/%d/', default='demo-artist.jpg', null=True, blank=True)

I tried this but didn't work

https://stackoverflow.com/questions/21797372/django-errno-13-permission-denied-var-www-media-animals-user-uploads
like image 603
Rahul Yadav Avatar asked Oct 19 '25 18:10

Rahul Yadav


2 Answers

I got the same error and debugged it using the shell

In your settings.py file: Change:

MEDIA_ROOT = BASE_DIR / '/media/'
# here, MEDIA_ROOT = '/media/'

To:

MEDIA_ROOT = BASE_DIR / 'media/'
# here, MEDIA_ROOT = 'path-to-project/media/'

I think this happens because you are trying to join your project level dir to the /media/ directory that exists in linux for mounting media. And would cause permission denied because root has the write permissions, you probably aren't running everything with sudo. So, instead you can remove the first \ to make the directory relative.

like image 73
Prashant Raj Avatar answered Oct 22 '25 07:10

Prashant Raj


mkdir --mode=777 -pv /home/rahul/.local/share/virtualenvs/music-69qL54Ia/{admin/main/artist/1/change,media/artists}

chmod -R 777 /home/rahul/.local/share/virtualenvs/music-69qL54Ia
like image 32
evan Avatar answered Oct 22 '25 06:10

evan