2

I am trying to start Django on Heroku. I looked around Stack Overflow, I tried different things, but I cannot figure it out. It looks similar to all the questions related to staticfiles problem on Django, unfortunately I don't know where the problem is. My project runs just fine with DEBUG = True, but when I change it to False, I get following traceback:

2020-11-09T13:13:42.801384+00:00 app[web.1]: Missing staticfiles manifest entry for 'order/css/open-iconic-bootstrap.min.css'

It happens on all of my apps that require staticfiles. I tried to find manifest.json, but it does not exist. So I think this is the issue.

Here are my relevant settings:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
#more ...
]
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') 
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

django_heroku.settings(locals())

Thanks for looking into this!

Sliwa
  • 61
  • 8
  • Can you confirm that you already tried with `heroku run python manage.py collectstatic` ? Do you have DISABLE_COLLECTSTATIC enabled on you settings ? If yes, disabling it might give you a lead on what's going wront. Maybe set DEBUG_COLLECTSTATIC=1 as well. – keoma Nov 12 '20 at 23:24
  • @keoma here is my configuration: `DEBUG_COLLECTSTATIC: 1` `DISABLE_COLLECTSTATIC: 0`. I tried toggling DISABLE_COLLECTSTATIC, but I could not see any difference. When I run collectstatic on heroku, I get `It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path` on all of my files resulting in `0 static files copied to /app/staticfiles, 742 unmodified, 2140 post-processed` – Sliwa Nov 14 '20 at 13:25
  • there might be a difference between `DISABLE_COLLECTSTATIC=0` and the variable not being set. Can you try with `heroku config:unset DISABLE_COLLECTSTATIC` ? Make sure the run build script and not just restart the script too. – keoma Nov 16 '20 at 11:27
  • @keoma That worked! Can you post it as an answer? – Sliwa Nov 20 '20 at 11:48

1 Answers1

2

@keome in the comments already gave you the first steps in debugging this issue so I won't repeat that here.

Notwithstanding the issues they raised (which should be looked at first), I think the key issue is that your whitenoise configuration isn't set up to generate a manifest. You probably want:

# Serve compressed and manifested static files
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"

The reason your current configuration would work with DEBUG=1 is that in debug mode, django will fall back to serve static files itself (neither secure nor efficient).

Second, make sure that you run collectstatic with the same storage configuration as you're running the server - I cant see what settings files you have, but it seems like you've collected static without a manifesting strategy, but you're trying to serve it with a manifesting strategy (and hence django is confused by why there isn't a manifest).

Incidentally, whitenoise by default creates a staticfiles.json file, not a manifest.json file, and it's served as a static file. So if your STATIC_URL = "/static/" then you can find the manifest at <your-domain>/static/staticfiles.json.

Edit - clarification

The reason the manifest of staticfiles isn't actually called manifest.json is because that'd conflict with the usual name of the manifest.json served as part of a Progressive Web App (spec here), which is kind of similar (in that it can serve as an instruction to the browser of where to find certain files) but not the same.

thclark
  • 4,784
  • 3
  • 39
  • 65
  • I have the STATICFILES_STORAGE line above, but I'm getting all the same symptoms. I can reproduce locally by setting DEBUG=False and doing collectstatic. I can see all the files in staticfiles, but Django is not serving them up (400 errors). Ideas of how to debug? – dfrankow Oct 17 '21 at 17:53
  • I had not properly installed whitenoise. (I forgot to add the middleware.) – dfrankow Oct 17 '21 at 17:57