Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django static_root in /var/www/... - no permissions to collectstatic

My STATIC_ROOT folder is specified to be /var/www/app_name/static, which is usually should not be accessed by non-sudo users.

It was created by a user with sudo rights. Now i am trying to collectstatic with my own user and it says "Permission denied...", not surprisingly.

How should I properly collect static files? Under which user?

like image 294
Denys Avatar asked May 12 '16 11:05

Denys


People also ask

How do I set up a static file in Django?

How to setup static files in Django. There are 3 main things to do: set STATIC_ROOT in settings.py. run python2.7 manage.py collectstatic (or python3.5 or python3.6 as appropriate) set up a Static Files entry on the PythonAnywhere Web tab. Optionally, you can also customise STATIC_URL, if you want to use a static URL prefix other than /static/.

What is static_root in Django?

STATIC_ROOT = os.path.join (BASE_DIR, 'staticfiles') STATIC_ROOT is the single root directory from where the Django application will serve the static files in production. While deployment you would typically want to serve the static files from the conventional /var/www/example.com

How to include static assets in a Django template?

Open base.html file and at the very top add {% load static % }. This tells Django to load static template tags inside the template so you use the {% static %} template filer to include different static assets. For example, in order to include the newly created base.css file in my template, I have to add this inside the <head> of the HTML document.

How do I import static files into static_root?

Run pythonX.Y manage.py collectstatic. This command (don't forget to replace "X.Y" with the version of Python your website uses) collects up all your static files from each of your app folders (including the static files for the admin app) and from any other folders you specify in settings.py, and copies them into STATIC_ROOT.


2 Answers

Take ownership of /var/www/app_name/static then put it back the way it was.

$ sudo chown -R myuser:myuser /var/www/app_name/static
$ python manage.py collectstatic
$ sudo chown -R root:root /var/www/app_name/static
like image 60
James Fenwick Avatar answered Sep 22 '22 12:09

James Fenwick


I think the best way you should add current user to group www-data

like image 22
Hùng Ng Vi Avatar answered Sep 25 '22 12:09

Hùng Ng Vi