avatarJen-Hsuan Hsieh (Sean)

Summary

The web content provides a comprehensive guide on deploying a Django application with PostgreSQL to Heroku, detailing the steps from setting up a PostgreSQL instance to seeding data and creating an administrator account.

Abstract

The article serves as a detailed walkthrough for developers looking to deploy a Django application integrated with a PostgreSQL database on the Heroku platform. It begins by acknowledging the limitations of free Heroku accounts, such as the application sleeping after 30 minutes of inactivity, but emphasizes the suitability of Heroku for learning and prototyping purposes. The guide is structured as a series of steps, including creating a PostgreSQL instance on Heroku, crafting a Procfile, modifying Django settings, migrating the database, seeding data, and creating an admin user via the Heroku CLI. The author, Sean, shares his personal notes and invites feedback, aiming to assist others in the software engineering community.

Opinions

  • The author, Sean, positions Heroku as a convenient platform for deploying server applications like those built with Flask or Django, especially for educational purposes or prototype development.
  • The use of Heroku's free tier is considered adequate for the purposes of learning and building prototypes, despite its limitations such as application sleep cycles.
  • The article is presented as a series, indicating an intention to cover the topic in depth and provide a comprehensive resource for building and deploying a ReactJS single-page application (SPA) with Django and Heroku.
  • The author encourages reader interaction by inviting feedback and suggestions, showing a commitment to continuous learning and improvement.
  • Sean's note-taking approach suggests a practical and hands-on learning style, which he shares with the community for mutual benefit.

Build Single page application with React and Django Part 1 - Deploy Django application to Heroku and migrate PostgreSQL

Introduction

The disadvantage for free user is that the application sleeps ever 30 minutes. However, it’s good enough for us to learn or build the prototype. I usually deploy applications on Heroku when I learn languages for building server like Flask or Django.

About this series

The target of this series is to build a ReactJS single page application(SPA) with Django API server and deploy on Heroku.

Agenda

The article is the note for deploying Django application with PostgreSQL. It includes following topics.

Suppose that we already have a Django application in the local site.

Create a PostgreSQL instance on Heroku

1.Select Resources and enter Heroku Postgres in the Add-ons box after creating a new application

copy right@A Layman

2.Choose Hobby Dev-Free then we will get a PostgreSQL instance

copy right@A Layman

Create Procfile in the application

  • Create a file called Procfile in the root of the application and put the following code in the file
web: gunicorn --pythonpath pizza pizza.wsgi
copy right@A Layman

Modify settings.py in the application

1.Install dj-database-url and gunicorn

pip install dj-database-url
pip install gunicorn

2.Create requirements.txt and list installed packages.

Django==3.0.3
gunicorn
dj-database-url==0.5.0

3.Edit settings.py

  • databases
DATABASES = {
    'default': dj_database_url.config(default=os.getenv('DATABASE_URL'))
}
copy right@A Layman
  • allowed hosts
ALLOWED_HOSTS = [
    'https://testets1234.herokuapp.com/'
]

Migrate the database from Heroku cli

1.Make sure we have installed Heroku cli

2.Login Heroku

heroku login

3.List apps

heroku apps
copy right@A Layman

4.Set remote Heroku URL

heroku git:remote -a testets1234

5.Push code to Heroku. It will also deploy application

git add .
git commit -m 'whatever you want comment'
git push heroku master

6.Migate the PostgreSQL

heroku run python manage.py migrate

Seed data to the remote database from Heroku cli

Suppose that you want to seed data from csv file and you have a table called products.

1.Create insert_data.py as the following example

2.Push code to Heroku.

git add .
git commit -m 'add insert_data.py'
git push heroku master

3.Run the script on Heroku

heroku run python insert_data.py

Create a administrator for Django admin from Heroku cli

  • Use the following command
heroku run python manage.py createsuperuser

Reference

Summary

Thanks for your patient. I am Sean. I work as a software engineer.

This article is my note. Please feel free to give me advice if any mistakes. I am looking forward to your feedback.

Please feel free to clap if this article can help you. Thank you.

You can also subscribe my page on Facebook.

Related topics

How to use the two-way binding in Knout.js and ReactJS?

Learn how to use SignalR to build a chatroom application

My reflection of :

IT & Network:

Database:

Software testing:

Debugging:

DevOps:

Django
Heroku
Postgresql
Deployment
Software Development
Recommended from ReadMedium