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?
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/.
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
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.
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.
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
I think the best way you should add current user to group www-data
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With