See the GitHub pages section on how to deploy your static site through GitHub pages.
Deployment
GitHub Pages
Heroku
Heroku is a cloud-based PaaS that allows developers to build, run, and scale web applications without managing the underlying server infrastructure. It abstracts away the complexities of deployment, letting you focus on writing code rather than configuring servers or networking.
You'd use Heroku when you want an easy, reliable way to deploy and host your web applications, especially for frameworks like Django and Flask.
Useful Documentation
On the Heroku Dashboard you can click on "Documentation" along the footer and it will take you to the dev centre with information on getting started, reference materials, tutorials which is very handle for referring back to during your deployment.
Dynos
Dynos in Heroku are Heroku managed Linux containers. The one's that we are most interested in are the web dynos that are containers where your application code runs. Hosting on Heroku is not free and they offer tiers of Dynos to pick from. The Eco Dyno will cover most of your personal development for only $5 a month.
Dynos can be selected from the billing section of your account settings and set in an app from the overview page.
Create a New App
To create a new app in Heroku, head to the Dashboard and click on "create new app". Add a unique app name with no spaces (use hyphens instead of spaces) and set your correction region. Then click on "create app" and you should see this app appear on your dashboard.
The Heroku CLI
The Heroku CLI is a command-line tool provided by Heroku that lets you manage your apps, dynos, add-ons, config variables, logs, and deployments directly from your terminal. The CLI integrates with Git so you can push your code, scale processes, view logs, run one-off commands. Using the CLI means you don't have to manage servers or infrastructure directly. It's especially convenient for Django projects as you can treat deployment almost like your git pushes. Despite it's ease of use, I usually configure my apps through the dashboard correctly and rely on the CLI for troubleshooting.
To install the CLI, first make sure you have Git installed and your project is under version control because Heroku will use a Git-remote to receive your code. Download and install the Heroku CLI for your OS. After installation, you can check that it has worked by opening your terminal and running:
heroku --version
You can access it in your terminal by running:
heroku login
It will prompt you to press any button to open a browser to confirm your login so just enter your account details there to authorise it. You can then see all possible commands with:
heroku
Then further information on each command can be seen by adding the command name and appending "help".
heroku command_name help
Most commands in the CLI will need to be app specific so will mostly have appended:
--app my-unique-app-name
Here are some common commands to use in the Heroku CLI that you will need at some point or another:
| Command | Use |
|---|---|
heroku apps
|
Show a list of your apps |
heroku apps:rename new_name --app old_name
|
Rename app |
heroku logs --tail --app app_name
|
View logs for an app named app_name |
heroku open --app app_name
|
Open the live site for an app named app_name |
Preparing Your Django Project
Before deploying your Django project on Heroku, certain changes and additions will need to be made. Firstly if you have a backend, you will need to have that hosted somewhere rather than locally with SQLite. This can be done through a Heroku Postgres addon.
Preparing Your Django Project: Installs
| Install Command | Action | Use |
|---|---|---|
pip3 install psycopg2-binary
|
Installs psycopg2 | Psycopg2 is a PostgreSQL adapter for Python letting Django talk to the database pythonically |
pip3 install gunicorn
|
Install Gunicorn | Gunicorn is a Python WSGI HTTP server. It runs your Django application in production and handles incoming web requests from the internet. |
pip3 install dj-database-url
|
dj-database-url | Converts a database url into a usable Heroku DATABASE_URL |
Preparing Your Django Project: Files
| File | Use |
|---|---|
| requirements.txt | Make sure you have an up to date requirements.txt file in your root directory that lists all dependencies so Heroku's Python backbuild knows what to install to run your webapp correctly |
| Procfile |
Create a Procfile file in your root directory (no extension) and have one line of code in it:web: gunicorn your_project_name.wsgiThis tells Heroku how to run the web process. |
Preparing Your Django Project: settings.py
Update your project settings.py file ALLOWED_HOSTS variable with the URL for your Heroku app. This can be found in the dashboard for your app, then go Settings and you can see it listed in your "Domains". It will look something like app_name5141515vsd1as6.herokuapp.com. Remember to leave out the "https://" when you add it.
ALLOWED_HOSTS = [
'localhost',
'127.0.0.1',
'webdevgrad-randomlettersandnumbers.herokuapp.com'
]
If using a database, import dj_database_url at the top. dj-database-url is a small Python utility library that parses a database URL string into Django's DATABASES configuration format. It simply takes a single string like postgres://username:password@hostname:5432/dbname and turns it into the structured dictionary Django expects in settings.py. Then update your DATABASE to check if there is a Heroku DATABASE_URL to use your database URL defined in your env.py file or to use the local one if in development.
# With other imports
import dj_database_url
# Database management
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
if 'DATABASE_URL' in os.environ:
# External database
DATABASES = {
'default': dj_database_url.parse(
os.environ['DATABASE_URL']
)
}
else:
# Local database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
Config Vars
When your site is deployed, it will not have access to your env.py file that is only stored locally on your system. The configuration variables is where you will transfer these keys to, to be kept secure on Heroku. They are found in Settings > Config Var in your app dashboard. Click "reveal config vars" to show them. Then go through and add all of your defined variables in env.py using the exact variable name and values. Add an extra DEBUG False variable here so that DEBUG is turned off in your deployed app.
Deploy Through GitHub
Now all that is left is to deploy your app from GitHub. In your app dashboard go to "Deploy" and here you can set your deployment method to GitHub, connect your correct repository and then either set it to automatically deploy when GitHub is updated or let you manually deploy when you want instead of after each change you make. With your app set up, you can see what it looks like by scrolling to the top and clicking on "Open app".