6

I am new in python and Linux and apologize in advance for any confusion. I am trying to collect my static files using

python manage.py collectstatic

but something error here are my tracebacks

> 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'

and I also try 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?

and here is my setting.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
] 
STATIC_ROOT = '/var/www/staticfiles'
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
Terry shu
  • 73
  • 1
  • 1
  • 4

4 Answers4

5

Give the permission necessary to the folder /var/www/staticfiles:

chmod -R 755 /var/www/staticfiles

As mentioned in the comments, is not recommended give permission 777 to all users , instead try changing the owner of the folder with chown -R your_user your_file

Mauricio Cortazar
  • 4,049
  • 2
  • 17
  • 27
  • thanks a lot. i try 755 but it doesn't work so i try 777 and it works – Terry shu Aug 27 '17 at 18:57
  • 1
    777 being the number of the beast (in octal), surely there's a less foot-gunnish answer? – Davis Herring Jun 17 '18 at 14:24
  • I put 755 but it doesn't worked for him, is another issue with the permission – Mauricio Cortazar Jun 17 '18 at 14:26
  • 1
    This is a dangerous solution to the stated problem! See here, the issue is likely related to the owner of the directory: https://stackoverflow.com/questions/25509774/django-file-upload-with-nginx-gunicorn-media-permissions – souldeux Jun 17 '18 at 14:34
  • 1
    Please also see the comments on this question for details as to why `chmod 777` is not the right thing to do here: https://stackoverflow.com/questions/8328481/chmod-777-to-a-folder-and-all-contents – souldeux Jun 17 '18 at 14:36
4

Try removing the slash in front of the var of your path.

So

STATIC_ROOT = '/var/www/staticfiles/

is changed to

STATIC_ROOT = 'var/www/staticfiles/'

or

STATIC_ROOT = os.path.join(BASE_DIR, 'var/www/staticfiles/')

The problem is that the path is not valid and therefore the permission is denied.

Whale
  • 49
  • 4
2

Rather than changing permissions, you can instead simply add the full path to the directory, e.g.:

STATIC_ROOT = os.path.join(BASE_DIR, "/var/www/staticfiles/")

Source

sc28
  • 1,163
  • 4
  • 26
  • 48
  • This may not be required , the issue is with permissions not being available to create DIR - Images in location = ```OSError: [Errno 13] Permission denied: '/var/www/staticfiles/images'``` . The DIR has been found , thus path need not be defined again. – Rohit Dhankar Oct 13 '19 at 11:20
0

Try making manage.py executable. This solved my problem :)

chmod +x manage.py