Forums

Django static files and STATIC_ROOT optimized for GitHub

For my setup I have my Django website on my Windows laptop, a GitHub repository, and a clone of the repo on PA. I plan to make changes from both the website (add/modify SQLite records) and the laptop (major design changes as well as db adds/mods) with regular syncs to the GitHub repo.

What's the best way to set up STATIC_ROOT?

I keep static files both in a project /static/ folder as well as application /static/ folders. To get up & running I followed the PA tutorial: I kept STATIC_DIR = os.path.join(BASE_DIR, "static"), added STATIC-ROOT = "/home/TitaniumVentures/website/static", and ran python3.6 manage.py collectstatic - which essentially copied all my static files to a folder outside my project folder of /home/TitaniumVentures/website/titanium. This was fine - everything worked - but it doesn't fit the "sync back up to the mothership" model.

I wanted to set STATIC_ROOT to the same os.path.join(BASE_DIR, "static") as STATIC_DIR, figuring that collectstatic would just copy the application files into the project directory - unfortunately this raised a django.core.exception "ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting"

What am I missing? -Mike

I think you have added the STATIC_ROOT directory to the STATICFILES_DIRS list. You don't need to do this.

The "collectstatic" command copies all static files from the app, and the files in STATICFILES_DIRS to STATIC_ROOT. So you have a circular copy if you add that path, hence the error. See http://stackoverflow.com/a/12161409/3233

I keep static files both in a project /static/

I don't think you're supposed to keep static files there. This is where they are collected. Put the files in an app static folder, and copy them with collectstatic.

I wondered that, too, but my STATICFILES_DIRS list only contains my STATIC_DIR variable. collectstatic seems to recognize the circularity anyway.

My best option seems to be to

a) keep my project/static/ and project/app/static folders the way they are, and reference them in STATICFILES_DIRS

b) create a new project/allstatic folder, and point STATIC_ROOT to it.

This way, all my static files live under the project umbrella, I can run collectstatic locally, and when everything syncs up through GitHub the PA servers will look to a dedicated allstatic folder for files. This assumes that python manage.py collectserver only collects files, and doesn't tweak any server-side settings. I'm happy with this solution - my static files aren't that large or abundant - it just seems to violate DRY in some way... Thanks Billy! -Mike

Afterthought: Above I mentioned running collectstatic on my local (dev) copy on a regular basis, and pushing updates to PA through GitHub. If collectstatic does a clean sweep each time (ie, overwrites all prior copies) - then my syncs will include all my static files every time. Has anyone else encountered this? I may be better off letting my STATIC_ROOT folder stay on PA, outside the sync, and schedule collectstatic to run periodically on the PA copy. Thoughts?

Personally I'd do what you're thinking of doing. I think of collected static files as something similar to compiled code -- something that's built from the source in the repository, rather than something to keep in the repository itself.