Deploying Django REST API with Heroku [GET request, infinite loading]

Viewed 1457

So I'm currently working on a project which looks like:

- project-d/
  - api/
  - app/
  - web/

We're focussing on deploying everything inside the api/ folder onto Heroku first. This is where the Django project is located.

Steps that I have taken so far:

  • Created a new node on Heroku
  • Added ENV variables (including: DISABLE_COLLECTSTATIC=1, since I don't have static files). My Database (PostgreSQL) is running on a Digital Ocean droplet inside a Docker container (the ports are open).
  • Connect the git project to Heroku (heroku login, heroku git:remote -a special-park)
  • Added the domain of the Heroku project in the ALLOWED_HOSTS in settings.py (see the file here: https://github.com/buzzzlightyear/special-park/blob/deployment/api/specialpark/settings.py)
  • I used Pipfile so there was no need for requirements.txt
  • Added runtime.txt containing python-3.7.6
  • Added Procfile containing web: gunicorn specialpark.wsgi --pythonpath=specialpark --log-file -
  • Now I pushed everything on to Heroku, since we only want the subdirectory to be deployed I ran this command: git subtree push --prefix api/ heroku master

Response:

➜  project-d git:(deployment) git subtree push --prefix api/ heroku master
git push using:  heroku master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 284 bytes | 284.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Python app detected
remote: -----> Requirements file has been changed, clearing cached dependencies
remote: cp: cannot stat '/tmp/build_88d222dddd3ed0c8949a8d8032e6beb6/requirements.txt': No such file or directory
remote: -----> Installing python-3.7.6
remote: -----> Installing pip
remote: -----> Installing dependencies with Pipenv 2018.5.18…
remote:        Installing dependencies from Pipfile.lock (7a8381)…
remote: -----> Installing SQLite3
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 73.2M
remote: -----> Launching...
remote:        Released v23
remote:        https://special-park.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/special-park.git
   c296867..e786aa7  e786aa7057e186e3da89faef8360c306675ce07d -> master

So it seems that the app has been deployed. So I opened Postman and tried to send a GET request to /users/car so it would return all the cars of that user.

But when I send the request with Postman, the request just keep loading and never finishes. I tried both

  • https://special-park.herokuapp.com/users/car/
  • https://special-park.herokuapp.com:8000/users/car/

But it doesn't send me a response back like it does when running the project locally.

Can someone spot the mistake? Here is the repo, I am working inside the deployment branch:

https://github.com/buzzzlightyear/special-park/tree/deployment

UPDATE

I've added two views

@api_view(["GET"])
@permission_classes([])
def welcome(request):
    return JsonResponse({ 'msg': 'Welcome to Special Park'})

@api_view(["GET"])
@permission_classes([])
def welcome_user(request):
    user = User.objects.get(id=1)
    return JsonResponse({ 'msg': 'Welcome to Special Park', 'user': user.username })

The first one works, but the second one fails. So I know it has to do with the database. I think the connection is never established. The Database is an PostgreSQL database running inside a docker container on a Digital Ocean droplet.

Does anyone know why this connection is not working?

1 Answers

The reason why was, that the connection between the Database on Digital Ocean and the Heroku app was never established. I deleted the Database on Digital Ocean and just let Heroku make a Database for me. Now it works great!

Related