How to deploy the Django code into Heroku server?

Submitted 3 years, 6 months ago
Ticket #213
Views 308
Language/Framework Django
Priority High
Status Closed

Hi Guys,

Please share detail level steps to push the django code from local to Heroku server?

As  I am new to Django , I have developed some small app where I want to deploy into Heroku server.

Also in the steps, please provide details about what has to install in my local ,etc..

Submitted on Oct 06, 20
add a comment

3 Answers

Follow the steps... This may Help you....

  1. In your terminal, use the "heroku login" command to log in to the Heroku CLI.

  2. "git clone https://github.com/heroku/python-getting-started.git" to copy a basic django skeleton project to your local.

  3. rename python-getting-started to whatever.

  4. cd into this directory.

  5. run the following command: "pip install -r requirements.txt" Note: Postgres must be properly installed in order for this step to work properly.

  6. run the following command: "python manage.py collectstatic"

  7. Install redis on Mac: "brew install redis"

  8. Start redis server: "redis-server&" (The & at the end is to run it as a background process)

  9. Test if Redis server is running: "redis-cli ping". If it replies “PONG”, then it’s good to go!

  10. Install celery: "pip install celery"

  11. Make a tasks.py file in your application directory with the following code:

     from celery import Celery
    
     app = Celery('tasks', broker='redis://localhost:6379/0')
    
     @app.task
     def add(x, y):
         return x + y
    
  12. "cd .." back into root directory.

  13. Run celery: "celery worker -A=location of tasks&"

  14. run: "python manage.py shell" in your root directory.

  15. As your tasks celery server has been started, you can now use it to run your task just by importing tasks.py script, e.g from Python interpreter interactive mode:

    import hello.tasks
    hello.tasks.add.delay(1, 1)
    

This should return an Async message.

  1. Push your local to heroku master.

** Note: If you run "celery worker -A=location of tasks.py&" and it gives the message:

consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 61]           
Connection refused.

Try restarting the redis server with the command: "brew services restart redis"

Also , for more You can refer to the below link.

https://www.codementor.io/@jamesezechukwu/how-to-deploy-django-app-on-heroku-dtsee04d4

Submitted 3 years, 6 months ago

I am not using celery. It just simple form app .you have provided more details which is something I am not aware at the moment.

- p789ahm 3 years, 6 months ago

@P789ahm

Please find below the details,

Create the App in Heroku  using below URL,

https://dashboard.heroku.com/apps

Step 1:

heroku login

Step 2:

Navigate to the Django Project folder where manage.py resides

Step 3:

git init

Step 4:

heroku git:remote -a <app_name>

Step 5:

Create Procfile in PROJECT_HOME dir

Step 6:

pip install gunicorn

Step 7:

pip install django-heroku

Step 8:

import django_heroku

Add the following import statement to the top of settings.py:

Step 9:

Then add the following to the bottom of settings.py:

django_heroku.settings(locals())

Step 10:

Django does not support serving static files in production. However, the fantastic WhiteNoise project can integrate into your Django application, and was designed with exactly this purpose in mind.

pip install whitenoise

Step 11:

Next, install WhiteNoise into your Django application. This is done in settings.py’s middleware section (at the top):

MIDDLEWARE_CLASSES = (
    # Simplified static file serving.
    # https://warehouse.python.org/project/whitenoise/
    'whitenoise.middleware.WhiteNoiseMiddleware',
)

Step 12:

Finally, if you’d like gzip functionality enabled, also add the following setting to settings.py

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Step 13:

If collectstatic failed during a build, a traceback was provided that will be helpful in diagnosing the problem. If you need additional information about the environment collectstatic was run in, use the DEBUG_COLLECTSTATIC configuration.

heroku config:set DEBUG_COLLECTSTATIC=1

Step 14:

Sometimes, you may not want Heroku to run collectstatic on your behalf. You can disable the collectstatic build step with the DISABLE_COLLECTSTATIC configuration:

heroku config:set DISABLE_COLLECTSTATIC=1

Step 15:

git add .

Step 16:

git commit -am "Initial commit"

Step 17:

git push heroku master

Check this and let me know if you need more info.

Submitted 3 years, 6 months ago

Thanks. will check and get back to you.

- p789ahm 3 years, 6 months ago


Verified
Submitted 3 years, 6 months ago

Thanks. will check and get back to you.

- p789ahm 3 years, 6 months ago

Thank you. Your blog really helped me to deploy it. But after deploy I am getting "Application 500 error"

- p789ahm 3 years, 6 months ago

Hi, run the command " heroku logs --tail --app your_app_name " and check the logs of your application(mostly this will help you in finding the error and resolving it accordingly)

- Jaanvi 3 years, 6 months ago

Thanks its resolved my issue.

- p789ahm 3 years, 6 months ago

Good to know that your issue is resolved!!

- Jaanvi 3 years, 6 months ago


Latest Blogs