Search code examples
djangoherokudjango-rest-frameworkgunicorn

Application error on deployed django rest framework app on heroku


I'm trying to deploy my django rest framework app on Heroku. I read many other similar questions but I'm confused. My app structure is not right or I'm missing something.

This is my structure on git:

 .gitignore  
 requirements.txt 
 src
    |
    --authorization
    --core
    --static
    --staticfiles
    --Procfile
    --manage.py
    --suacm
        |
        ---asgi.py
        ---settings.py
        ---urls.py
        ---wsgi.py

authorization and core are apps under my django project. there wasn't static or staticfiles before heroku deploy. But it automatically created staticfiles. Then I also created static and followed instructions to make it work via changes in settings.py. It'd be awesome if someone help me figure out my problem on heroku and why it doesn't work.

This is Procfile:

web: gunicorn suacm.wsgi
web: gunicorn suacm:app

When I run app with this command my app works fine and run locally:

gunicorn suacm.wsgi:application

But I couldn't solve the error in deployed app.

With

heroku logs --tail

I receive errors starts like below:

2021-05-21T14:32:20.000000+00:00 app[api]: Build succeeded
2021-05-21T14:32:26.505788+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=suacm.herokuapp.com request_id=dfeba2ff-fa7d-4dfb-9337-706f50d286dc fwd="82.222.237.15" dyno= connect= service= status=503 bytes= protocol=https
2021-05-21T14:32:26.882608+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=suacm.herokuapp.com request_id=292525ae-bc4e-49c7-b979-e455bbfd6b95 fwd="82.222.237.15" dyno= connect= service= status=503 bytes= protocol=https

When I run this

heroku ps:scale web=1 --app suacm

I get this:

Scaling dynos... !
 ▸    Couldn't find that process type (web).

And finally when I try to run heroku locally under src folder with this command

src % heroku local web

I get this response:

[INFO] Starting gunicorn 20.1.0
[INFO] Listening at: http://0.0.0.0:5000 (35246)
[INFO] Using worker: sync
[INFO] Booting worker with pid: 35247
Failed to find attribute 'app' in 'suacm'.
[INFO] Worker exiting (pid: 35247)
.
.

If needed, this is my requirements.txt file:

django_environ==0.4.5
djangorestframework_simplejwt==4.6.0
django_filter==2.4.0
Django==3.1.3
djangorestframework==3.12.4
environ==1.0
PyJWT==2.1.0
gunicorn==20.1.0
django-on-heroku==1.1.2
whitenoise==5.2.0

This is my first time deploying a django app. I hope I gave enough information. Just ask if there is more information needed.


Solution

  • Put your Procfile in the root directory, not in the src and

    change this

    web: gunicorn suacm.wsgi
    

    to

    web: gunicorn src.suacm.wsgi
    

    Then why it work on a local server?

    In your local setup, you are launching the server from the src directory which is not the root directory!
    But the in the production Heroku launches the application from the root directory, since there is no Procfile in the root directory you are getting these errors.