Django OSError:[Errno 13] 权限被拒绝
Posted
技术标签:
【中文标题】Django OSError:[Errno 13] 权限被拒绝【英文标题】:Django OSError: [Errno 13] Permission denied 【发布时间】:2018-02-05 01:16:53 【问题描述】:我是 python 和 Linux 的新手,如有任何混淆,请提前道歉。 我正在尝试使用
收集我的静态文件python manage.py collectstatic
但这里有些错误是我的回溯
> Copying '/var/www/Django/myweb/static/images/test.jpg'
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/test01/Django/VENV/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/home/test01/Django/VENV/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/test01/Django/VENV/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/test01/Django/VENV/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/test01/Django/VENV/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 199, in handle
collected = self.collect()
File "/home/test01/Django/VENV/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 124, in collect
handler(path, prefixed_path, storage)
File "/home/test01/Django/VENV/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 364, in copy_file
self.storage.save(prefixed_path, source_file)
File "/home/test01/Django/VENV/local/lib/python2.7/site-packages/django/core/files/storage.py", line 54, in save
return self._save(name, content)
File "/home/test01/Django/VENV/local/lib/python2.7/site-packages/django/core/files/storage.py", line 321, in _save
os.makedirs(directory)
File "/home/test01/Django/VENV/lib/python2.7/os.py", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/var/www/staticfiles/images'
我也试试sudo python manage.py collectstatic
>File "manage.py", line 17, in <module>
"Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
这是我的设置.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = '/var/www/staticfiles'
【问题讨论】:
【参考方案1】:尝试使 manage.py 可执行。这解决了我的问题:)
chmod +x manage.py
【讨论】:
【参考方案2】:尝试删除路径的var
前面的斜杠。
所以
STATIC_ROOT = '/var/www/staticfiles/
改为
STATIC_ROOT = 'var/www/staticfiles/'
或
STATIC_ROOT = os.path.join(BASE_DIR, 'var/www/staticfiles/')
问题是路径无效,因此权限被拒绝。
【讨论】:
【参考方案3】:您可以简单地添加目录的完整路径,而不是更改权限,例如:
STATIC_ROOT = os.path.join(BASE_DIR, "/var/www/staticfiles/")
Source
【讨论】:
这可能不是必需的,问题在于无法在 location =OSError: [Errno 13] Permission denied: '/var/www/staticfiles/images'
中创建 DIR-Images 的权限。已找到 DIR,因此无需重新定义路径。【参考方案4】:
授予文件夹/var/www/staticfiles所需的权限:
chmod -R 755 /var/www/staticfiles
如 cmets 中所述,不建议将权限 777
授予所有用户,而是尝试使用 chown -R your_user your_file
更改文件夹的所有者
【讨论】:
非常感谢。我尝试了 755 但它不起作用所以我尝试了 777 并且它有效 777 是野兽的编号(以八进制表示),肯定有一个不那么夸张的答案吗? 我放了 755 但它对他不起作用,是权限的另一个问题 这是对所述问题的危险解决方案!看到这里,问题可能与目录的所有者有关:***.com/questions/25509774/… 关于为什么chmod 777
在这里不适合做的详细信息,请参阅这个问题的cmets:***.com/questions/8328481/…以上是关于Django OSError:[Errno 13] 权限被拒绝的主要内容,如果未能解决你的问题,请参考以下文章
OSError: [Errno 2] 在 Django 中使用 python 子进程时没有这样的文件或目录
OSError: [Errno 13] Permission denied: '/dev/ttyACM0' - using pyserial from Python to Arduino
OSError:[Errno 2]在Django中使用python子进程时没有这样的文件或目录
Tensorboard: OSError: [Errno 22] Invalid argument
OSError:[Errno 26] 文本文件忙:'baremetrics'
如何解决:OSError: Unable to create file (unable to open file: name = ‘. et_classification.h5‘, errno = 2