Django无法上传到777权限文件夹

时间:2018-07-09 14:19:38

标签: python django linux apache centos

将文件上传到媒体文件夹时出现错误消息。 我已经将所有内容设置为777,并将所有者更改为apache。 加载媒体和静态文件就可以了。 我正在使用带有httpd服务的centos7。 请帮我弄清楚。

有关其他信息,可以通过硬代码处理wirte文件。

The error looks like

这是错误消息:

Traceback:

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  552.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  224.             return view(request, *args, **kwargs)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/contrib/admin/options.py" in change_view
  1512.         return self.changeform_view(request, object_id, form_url, extra_context)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/contrib/admin/options.py" in changeform_view
  1409.             return self._changeform_view(request, object_id, form_url, extra_context)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/contrib/admin/options.py" in _changeform_view
  1449.                 self.save_model(request, new_object, form, not add)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/contrib/admin/options.py" in save_model
  980.         obj.save()

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/contrib/auth/base_user.py" in save
  80.         super(AbstractBaseUser, self).save(*args, **kwargs)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/db/models/base.py" in save
  808.                        force_update=force_update, update_fields=update_fields)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/db/models/base.py" in save_base
  838.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/db/models/base.py" in _save_table
  902.                       for f in non_pks]

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/db/models/fields/files.py" in pre_save
  296.             file.save(file.name, file.file, save=False)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/db/models/fields/files.py" in save
  94.         self.name = self.storage.save(name, content, max_length=self.field.max_length)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/core/files/storage.py" in save
  54.         return self._save(name, content)

File "/opt/ezadmin/env/lib/python2.7/site-packages/django/core/files/storage.py" in _save
  347.                     fd = os.open(full_path, flags, 0o666)

Exception Type: OSError at /ezadmin/user/1/change/
Exception Value: [Errno 13] Permission denied: '/opt/ezadmin/media/users/hqdefault.jpg'

2 个答案:

答案 0 :(得分:1)

您需要通过SELinux更改权限

chcon -Rv --type=httpd_sys_rw_content_t /var/html/myweb/uploads

答案 1 :(得分:0)

使用django开发服务器后,使用apache的陷阱...

要检查的事情:

1)确保已为MEDIA文件夹设置了别名,以便正确路由www.derf.com/media

 Alias /media/ /var/www/derf/media/

2)确保该目录具有适当的APACHE权限。 Apache不会因为人们而盲目地遵循操作系统级别的权限。

<Directory /var/www/derf/media/
    Require all granted
</Directory>