Django无法上传到777权限文件夹
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django无法上传到777权限文件夹相关的知识,希望对你有一定的参考价值。
将文件上传到媒体文件夹时收到错误消息。我已经将所有内容设置为777允许并将所有者更改为apache。加载媒体和静态文件很好。我使用centos7与httpd服务。请帮我解决一下。
有关其他信息,通过硬编码写入文件工作正常。
这是错误消息:
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'
答案
你需要通过SELinux更改权限
chcon -Rv --type=httpd_sys_rw_content_t /var/html/myweb/uploads
另一答案
啊使用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>
以上是关于Django无法上传到777权限文件夹的主要内容,如果未能解决你的问题,请参考以下文章
在PHP中,move_uploaded_file将文件上传到一个目录,但无法上传到另一个目录?
为啥 NSURLSession uploadTaskWithRequest:fromData: 无法上传到 php 服务器?