Reputation: 363
I turned debug to False in my settings.py file (note that before I turned it to false, everything was workign perfectly) and when I ran git push heroku master
and went to my website, the home page was working but that wasn't the case for all of my pages: on some, I got an error saying Server Error (500)
Here is my settings.py
code :
DEBUG = False
ALLOWED_HOSTS = ["hacka-labs.herokuapp.com"]
What I have noticed is that the urls where I pass an id aren't working
Please help me if you know the answer
Upvotes: 5
Views: 8537
Reputation: 1
I had the same error while deploying react and django project on heroku. I had been debugging for 2 days, doing all things like allowed host, staticfiles, json, whitenoise things but that still didn't solve the problem.
If your browswer throws error 500 then set DEBUG=True
in settings.py
. If it throws a TemplateDoesNotExist error at index.html then changing views.py may help solve that. Don't forget to setDEBUG=False
before deploying.
I solved it by changing some code in views.py.
def index(request):
return render(request,'ur_app_name.html')
TO
def index(request):
some_variable_name=TemplateResponse(request,'ur_app_name.html',{})
return some_variable_name
Dont't forgot to import this on top of views.py
from django.template.response import TemplateResponse
I don't know what exact problem I was facing, I guess it's an httpResponse thing because server needs http request and our local machine does not need it.
Some key points points for this error:
ALLOWED_HOST
in settings.py
fileSTATIC_ROOT=os.path.join(BASE_DIR, 'staticfiles')
in settings.py
, this maybe needed when heroku throws a
collecticstatic
error but I am not sure. If it still throws the
same error then disable collectstatic=1
on command (you can find
the exact command for this in the debug log) which does not create
any problem.whitenoise.middleware.WhiteNoiseMiddleware
, in MIDDLEWARE
in settings.py
, this might help.This is the github repository where I upload my heroku project you can find the config files here which maybe helpful.
If your project uses react and django and above suggestions didn't help this may help:
.gitignore
files, install all requirement.txt
files
and copy package.json
file in root directory.set "build": "cd <project folder name>&& react-scripts build"
, in scripts in package.json
My index.html and manifest.json is in the github repo mentioned above.
The 'staticfiles' directory was created in the root folder using '$ python manage.py collectstatic
'. Try copying my index.html
file if your project doesn't have one.
That's all.
The herokuapp link :https://sainterface.herokuapp.com/
Upvotes: 0
Reputation: 35
I had the same error.
I solved issue by removing import django heroku
in settings.py
Upvotes: 0
Reputation: 1127
I had a similar problem and it's hard to figure out when DEBUG is set to False. When I reconfigured it back to DEBUG = True
everything worked fine again. So the problem was
You don't generally see this error in development because when
DEBUG = True
, ManifestStaticFilesStorage switches to non-hashed urls. https://stackoverflow.com/a/51060143/7986808
The solution to the problem in your case may differ from mine. But you can find the problem by changing the logging method of your Django project, so you can see more info in the command line when running heroku logs -t -a <heroku-app>
even in DEBUG = False
mode.
Change your django logging settings in settings.py
as follow:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': ('%(asctime)s [%(process)d] [%(levelname)s] '
'pathname=%(pathname)s lineno=%(lineno)s '
'funcname=%(funcName)s %(message)s'),
'datefmt': '%Y-%m-%d %H:%M:%S'
},
'simple': {
'format': '%(levelname)s %(message)s'
}
},
'handlers': {
'null': {
'level': 'DEBUG',
'class': 'logging.NullHandler',
},
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
}
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
},
'django.request': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': False,
},
}
}
Then run heroku logs -t -a <heroku-app>
and open the url where you previously got 500 Error you will find out the problem in logs. Hope this will help you.
In case there are some static files missing just switch your static root in settings.py
as follow STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
. Running heroku run python manage.py collectstatic -a <heroku-app>
creates staticfiles directory.
Upvotes: 9