Hitting 500 error on django with debug=False even with ALLOWED_HOSTS=["*"]
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Flying Over Ancient Lands
--
Chapters
00:00 Hitting 500 Error On Django With Debug=False Even With Allowed_hosts=[&Quot;*&Quot;]
01:33 Accepted Answer Score 14
02:56 Answer 2 Score 2
03:43 Answer 3 Score 0
04:24 Answer 4 Score 0
05:18 Thank you
--
Full question
https://stackoverflow.com/questions/5933...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #python3x
#avk47
ACCEPTED ANSWER
Score 14
So, the issue was related to a SINGLE ICON NOT LOADING, the path was off but due to the way Django handles static files (which is honestly dumb) when DEBUG=True I didn't catch it, and when DEBUG=False there was no traceback.
A neat trick I learned was you can force Django to give you the logging information you need by attaching an explicit logger in your main settings.py like so:
import logging
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
},
},
}
So three things I learned in my 6 hours of debugging you are in my situation:
- Set DEBUG = False right away in your development cycle, it will force you to configure collectstatic properly.
- Heroku's documentation for getting your app setup lies about how to configure WhiteNoise properly, so here is the real configuration:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = '.storage.WhiteNoiseStaticFilesStorage' # Read point 3 for details about this
- You have to subclass WhiteNoises default configuration locally to remove Djangos built in manifest_strict attribute (got that from here: https://stackoverflow.com/a/51580328/11602400)
from whitenoise.storage import CompressedManifestStaticFilesStorage
class WhiteNoiseStaticFilesStorage(CompressedManifestStaticFilesStorage):
manifest_strict = False
ANSWER 2
Score 2
I solved mine by
Put
DEBUG=Truein theproject/settings.pyRun
$ python manage.py collectstatic
Any error in your static files will appear in your terminal if there is any. Try solving that error before you proceed.
Go into your main
project/settings.pyAnd instead of making a generally allowed host by using"*". Replace it with your localhost link Eg:127.0.0.1.Go into your browser's history and delete recent cookies and cache.
Refresh your projects and that's all.
ANSWER 3
Score 0
Adding an answer since this just took me a couple hours to debug and this will maybe save someone else from the same problem.
I was only getting SERVER ERROR 500 on certain admin pages. Some models would work fine, others didn't. I suspected a third-party module at that point.
This problem showed up for me because I am using django-nested-inline and there was a bug in the version installed via pip that couldn't locate a copy of jQuery that is necessary to modify the admin pages to handle the nested inlines.
This bug was fixed so I needed to remove the version I was using and then install the fixed version directly from Github.
ANSWER 4
Score 0
Adding an answer because this stumped me for a while and a lot of answers, whilst they do apply to collect your static files the server error 500 can also occur if you have any files/images/CSS/js reference in HTML that isn't actually present in your project directory. For example if you reference <link href="{% static 'app/css/newStyle.css' %}" rel="stylesheet" /> and that file does not exist then you'll get this error.
Best way to check which files aren't actually present is to run in your local environment python manage.py runserver and load pages, to see where there is a 404 error in the terminal,
It will probably look like this:
"GET /static/app/css/newStyle.css HTTP/1.1" 404 1876
Then you can omit the reference to this file if not needed or locate where to add it in.