Errno-13权限被拒绝:'/ media /-Django

时间:2020-10-01 12:20:02

标签: python django ubuntu django-models permission-denied

我在ubuntu中使用Django 3.1,

上传媒体文件时出现错误

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

此代码在Windows中有效,但在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)

我尝试过但是没用

https://stackoverflow.com/questions/21797372/django-errno-13-permission-denied-var-www-media-animals-user-uploads

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

也许您忘记了将MEDIA_ROOT添加到您的urls.py。
有关更多信息,请查看docs

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

注意:这不适合生产使用。如果是这样,您可以签出docs

答案 2 :(得分:0)

我遇到了相同的错误,并使用shell

对其进行了调试

在您的settings.py文件中: 更改:

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

收件人:

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

我认为发生这种情况是因为您试图将your project level dir加入Linux中用于装载媒体的/media/目录中。并且由于root具有写权限而导致权限被拒绝,您可能没有使用sudo运行所有内容。因此,您可以删除第一个\来使目录相对。