How to deploy Django app in Heroku

You just create a Django app, you want to publish it and show it to public. Heroku is one of the popular place you can host your site there. But you need to know how to deploy it. Here is the step-by-step procedure.

First you need to login to the Heroku

(foodtasker) tiger:foodtasker xxxxx$ heroku login
Enter your Heroku credentials.
Email: xxxxx@gmail.com
Password (typing will be hidden): 
Logged in as xxxxx@gmail.com
(foodtasker) tiger:foodtasker xxxxx$ 

Then create project at Heroku

(foodtasker) tiger:foodtasker xxxxx$ heroku create
Creating app... done, ⬢ thawing-reaches-94509
https://thawing-reaches-94509.herokuapp.com/ | https://git.heroku.com/thawing-reaches-94509.git
(foodtasker) tiger:foodtasker xxxxx$ 

You need to install “gunicorn” for web server running at Heroku

(foodtasker) tiger:foodtasker xxxxx$ pip install gunicorn==19.6.0
Collecting gunicorn==19.6.0
  Downloading gunicorn-19.6.0-py2.py3-none-any.whl (114kB)
    100% |████████████████████████████████| 122kB 5.1MB/s 
Installing collected packages: gunicorn
Successfully installed gunicorn-19.6.0
(foodtasker) tiger:foodtasker xxxxx$ 

Check and see what applications you have installed in the project

(foodtasker) tiger:foodtasker xxxxx$ pip freeze
Django==1.10
gunicorn==19.6.0
olefile==0.44
Pillow==4.0.0
(foodtasker) tiger:foodtasker xxxxx$ 

Then create a textfile “requirements.txt” and paste the list of applications you have installed in the project

You also need to create textfile “runtime.txt” and specific which python version you want to use in Heroku. In my case, I am going to use python 3.6.0 for local and Heroku

python-3.6.0

In the last, you need to create textfile “Procfile” to specific what application you want to start at Heroku

web: gunicorn foodtasker.wsgi --log-file -

In the settings.py, we need to specific the STATIC_ROOT and which database will be used in the Heroku

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)

In the wsgi.py, you need to tell Heroku using whitenoise to serve static file

# Use whitenoise package to serve static files on Heroku
from whitenoise.django import DjangoWhiteNoise

application = DjangoWhiteNoise(application)

Add all the files to the GIT

(foodtasker) tiger:foodtasker xxxxx$ git add .
(foodtasker) tiger:foodtasker xxxxx$ git commit -m "fix"
[master 5b4bbd4] fix
 1 file changed, 1 insertion(+), 1 deletion(-)
(foodtasker) tiger:foodtasker xxxxx$ 

Push the master branch from local to Heroku

(foodtasker) tiger:foodtasker xxxxx$ git push heroku master
Counting objects: 63, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (55/55), done.
Writing objects: 100% (63/63), 332.45 KiB | 0 bytes/s, done.
Total 63 (delta 11), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 

Run Django migrate in the Heroku

(foodtasker) tiger:foodtasker xxxxx$ 
(foodtasker) tiger:foodtasker xxxxx$ heroku run python manage.py migrate
Running python manage.py migrate on ⬢ thawing-reaches-94509... up, run.1421 (Free)
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, foodtaskerapp, sessions
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying foodtaskerapp.0001_initial... OK
  Applying sessions.0001_initial... OK
(foodtasker) tiger:foodtasker whoismikechan$ 

Create super user in Heroku which it is used to manage the project

(foodtasker) tiger:foodtasker xxxxx$ heroku run python manage.py createsuperuser
Running python manage.py createsuperuser on ⬢ thawing-reaches-94509... up, run.2410 (Free)
Username (leave blank to use 'u9090'): admin
Email address: xxxxx@gmail.com
Password: 
Password (again): 
Superuser created successfully.
(foodtasker) tiger:foodtasker xxxxx$ 

Open the application in the Heroku

(foodtasker) tiger:foodtasker xxxxx$ heroku open

Checking the log at Heroku, you can use command “heroku logs”

(foodtasker) tiger:foodtasker xxxxx$ heroku logs 
2017-02-18T17:22:06.601621+00:00 heroku[router]: at=info method=GET path="/static/js/bootstrap.min.js" host=thawing-reaches-94509.herokuapp.com request_id=45a8c4d1-75b0-4433-bb6c-fd8e35bbd7ab fwd="210.6.84.60" dyno=web.1 connect=1ms service=3ms status=304 bytes=144
Advertisements

About whoismikechan

I am just a guy who has a family, working daily with my daily work, but always dream about to have my own startup company.
This entry was posted in Heroku. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s