2

Update: looks like this is being cause by the django-heroku package and specifically the inherited whitenoise package, which in docs says your supposed to put

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

at the end of settings file, which I never did but it still enforces root being 'staticfiles' folder instead of assets folder

Original Post:

When I run python manage.py collectstatic it saves to a folder called staticfiles

I must have done something to make this happen, but I've searched for staticfiles and found no reference to it except 'django.contrib.staticfiles'.

Here is my settings.py:

INSTALLED_APPS = [
    #some apps
    'django.contrib.staticfiles',
#some more apps

    'tz_detect',
]

# some more code

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]
VENV_PATH = os.path.dirname(BASE_DIR)

STATIC_ROOT = os.path.join(BASE_DIR, 'assets')

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(VENV_PATH, 'media_root')

Expected outcome was for when python manage.py collectstatic is run, that an assets folder would be created, but this never happens.

The staticfiles folder in addition to files from static folder also contains a tz_detect folder (from static assets from 3rd party package), an admin folder, and a staticfiles.json

middleware section of settings.py

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    # some more middleware
    'tz_detect.middleware.TimezoneMiddleware',  
]
Padoga
  • 495
  • 3
  • 18
  • Can you provide a link to the `tz_detect` package? – Lord Elrond Oct 14 '19 at 01:11
  • Sure, but I think whitenoise package is the issue http://whitenoise.evans.io/en/stable/django.html . Here is tz_detect package https://github.com/adamcharnock/django-tz-detect – Padoga Oct 14 '19 at 01:13
  • Thanks, I'm looking into this right now. Can you add an update showing your `MIDDLEWARE`? – Lord Elrond Oct 14 '19 at 01:17
  • update question showing middleware change – Padoga Oct 14 '19 at 01:20
  • 1
    I ran `grep -nr 'staticfiles' path/to/tz-detect` and a few other combinations, and found nothing, so `tz-detect` is unrelated to the issue. That being said, I think `whitenoise` might be causing the issue, but I can't seem to recreate your problem, so I think there is something important that isn't included in your question. Are *all* 3rd party apps/middleware included in `INSTALLED_APPS` and `MIDDLEWARE` (not including default packages)? – Lord Elrond Oct 14 '19 at 01:48
  • I include all third party packages in my comment. Except for I added import django_heroku at top of my settings.py file. And django_heroku.settings(locals()) at bottom of settings.py https://devcenter.heroku.com/articles/django-app-configuration – Padoga Oct 14 '19 at 02:45
  • whitenoise is a dependency of django_heroku – Padoga Oct 14 '19 at 02:46

1 Answers1

1

It appears that it isn't possible to change the name of your STATIC_ROOT using django_heroku, without monkey patching the package.

The line django_heroku.settings(locals()) takes all the local variables (ie STATIC_ROOT) and passes them into the settings function found at django_heroku/core.py.

If you take a look at line 89:

config['STATIC_ROOT'] = os.path.join(config['BASE_DIR'], 'staticfiles')

You will see that whatever value you set for STATIC_ROOT, the package will override it with staticfiles.

Note that the following may have unintended consequences, and you shouldn't do them.

Here are 2 monkey patch solutions:

  • change the value of STATIC_ROOT after you call django_heroku.settings(locals()):
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
  • modify django_heroku/core.py line 89: to the following:
if 'STATIC_ROOT' not in config:
    config['STATIC_ROOT'] = os.path.join(config['BASE_DIR'], 'staticfiles')

this will prevent djano_heroku from overriding the STATIC_ROOT if you have already defined it.

Again, I don't recommend doing this because there might a good reason that Heroku forces you to use the name staticfiles, and this might cause your server to break, or worse, parts of your server might silently fail (which means debugging will be a nightmare).

Lord Elrond
  • 13,430
  • 7
  • 40
  • 80