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:
- Make sure you mention your domain name in
ALLOWED_HOST
in settings.py
file
- set
STATIC_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.
- set
whitenoise.middleware.WhiteNoiseMiddleware
, in MIDDLEWARE
in settings.py
, this might help.
- If your project uses react and django then choose python only on add build pack on heroku. Do not choose node.js.
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:
- check
.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/