DisallowedHost error (ALLOWED_HOSTS) when collectstatic was missed

I’ve been building a django website and deploying to azure web app and I ended up in a mess of errors. I have eventually found my way out but the main error I was getting was very misleading and didn’t help me resolve the true issue. I can provide more details on the setup but the main parts relevant to this post are that I’m using django version 3.2, whitenoise version 5.3 to handle static files and I was deploying it with DEBUG=False setting and a value for ALLOWED_HOSTS (driven via environment variables so I could change it through config on the site).

The first error I kept seeing after the site served a 500 internal error page was of this sort:
[XXXX] ERROR [django.security.DisallowedHost:99] Invalid HTTP_HOST header: ‘’. You may need to add ‘’ to ALLOWED_HOSTS.

I tried extending the config but would get another IP and I was sure I’d gotten this working in azure without needing an exhaustive list of IP addresses (plus I think they can change!) So to debug this I tried just setting the value to ‘*’ for ALLOWED_HOSTS. This then showed an error like:

[XXXX] ERROR [django.security.DisallowedHost:99] Invalid HTTP_HOST header: ‘somesite.azurewebsites.net’. You may need to add ‘somesite.azurewebsites.net’ to ALLOWED_HOSTS.

Yet when I did add that back in it goes back to the first error with IP addresses. Plus I thought * was universal and should just let it run. I went around in a lot of circles until I stumbled across static files missing. I ran “python manage.py collectstatic” as part of my build and deploy and set ALLOWED_HOSTS back to the correct value and the site worked!

I’ve re-check the logs and there is only this error and nothing else which hints that it was the lack of static files which was the true problem. I haven’t found anything like this with searching forums and I haven’t yet checked if this is still true on the latest version of django - I thought I’d reach out to ask on here first if this is a fixed issue or not.
I know it was a daft error of mine to miss off a key step in deployment but the error messaging didn’t indicate that to be the issue at all and led me to spend a long time investigating ALLOWED_HOSTS values which wasn’t even the issue.

You might be able to test this in your local environment. Set up a production-like environment on your computer and see what happens.

If anything, I’d be guessing it’s something within whitenoise, since in a Django deployment without it, you would be relying upon the web server to serve static files - the ALLOWED_HOSTS setting wouldn’t factor in to it at all.