Django-使用MEDIA_ROOT实现models.FileField引发权限错误

aless80

我正在尝试实现models.FileField(作为旁注models.ImageField抱怨我没有Pillow,但是一旦解决,我将尝试安装它)。当我使用MEDIA_ROOT并尝试从管理员上传文件时,出现此错误:

/ admin / resume / project / 9 / change /中的PermissionError

[Errno 13]权限被拒绝:“ / resume”

请求方法:POST请求URL:http : //127.0.0.1 :8001 / admin/ resume/project/9 / change/ Django版本:2.0.3异常类型:PermissionError异常值:

[Errno 13]权限被拒绝:“ / resume”

异常位置:makedirs中的/home/kinkyboy/virtualenv/djangoresume/lib/python3.4/os.py,第237行Python可执行文件:/ home / kinkyboy / virtualenv / djangoresume / bin / python3 Python版本:3.4.3 ..

这是代码的相关部分。

### project/settings.py
RUNNING_DEVSERVER = (len(sys.argv) > 1 and sys.argv[1] == 'runserver')
DEBUG = RUNNING_DEVSERVER
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
        os.path.join(BASE_DIR, '/static')
]
MEDIA_ROOT = '/resume/'
MEDIA_URL = '/resume/'

### project/urls.py
..
from django.conf import settings
from django.conf.urls.static import static
  urlpatterns = [
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
..

### app/models.py
class Project(models.Model):
  ..
  image = models.FileField(upload_to='img/',blank=True)

### app/index.html
<p>Use this below: {{proj.image.url}}</p>
<div class="item active" style="background-image: url('{% static '<use the image url>' %}');">

我的项目结构:

Project
├── project
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── __pycache__
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
└── resume
    ├── admin.py
    ├── apps.py
    ├── fixtures
    ├── __init__.py
    ├── migrations
    ├── models.py
    ├── __pycache__
    ├── static
    │   └── resume
    │       ├── img
    │       ├── ...
    ├── templates
    ├── tests.py
    └── views.py

编辑:这解决了我的问题

### project/settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, "resume/")
MEDIA_URL = '/'  #this sets the relative path from /app for uploaded files

### app/models.py
image = models.FileField(upload_to='static/resume/img/',blank=True)

### app/index.html
<div class="item active" style="background-image: url('{{ proj.image.url }}');">
乔纳森

根据FAQ中有关图像和文件字段文档,您需要:

  1. 在MEDIA_ROOT中指定目录的完整路径
  2. 确保运行Web服务器的用户确实具有对MEDIA_ROOT目录指定的目录的写访问权

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Django MEDIA_URL和MEDIA_ROOT

要使用Django FileField还是FilePathField?

django-fsm:权限未引发异常

Django / Python:使用models.FileField保存文件时如何更改文件名?

Django使用替换的Auth模型引发操作错误

Django 2.1 startproject引发奇怪的错误

Django MEDIA_ROOT指向网络驱动器

Django表单FileField错误'此字段为必填项'

我如何使用LoginRequiredMixin在基于类的视图中引发django错误消息?

Errno 13尝试将映像保存到media_root时,权限被拒绝

在Django中使用默认URL值会引发404错误

选择引发验证错误的Django Admin

Django:MEDIA_ROOT错误:_getfullpathname:路径应为字符串,字节或os.PathLike,而不是元组

Django 1.4-django.db.models.FileField.save(filename,file,save = True)使用非ascii文件名产生错误

使用FileField上传到django中的属性

django media_root不起作用

无法使用MEDIA_ROOT和MEDIA_URL在Django中上传图片

django:引发类似于EmailField的错误

使用django验证带有引发错误的表单

Django models.py错误

Django保存覆盖引发主要重复错误

无法在Django中使用MEDIA_ROOT上传图片

Django:使用 mod_wsgi 创建的 SOCK 文件的权限错误

Django 模板 - 来自 media_root 的图像呈现在主页中,但不是其他页面

Django Rest Framework 使用`permissions.IsAuthenticated` 引发错误

如何修复来自 Django MEDIA_ROOT 的断开链接?

python django中MEDIA_ROOT(媒体文件)和STATIC_ROOT(静态文件)的真正区别以及如何正确使用它们

为什么不应该在 Django 的生产级别使用 MEDIA_ROOT 和 MEDIA_URL?

Django in_bulk() 使用 distinct() 引发错误