Below are the steps I took to deploy the site to Heroku and any console commands required to initiate it.
- Create a new repository in GitHub and clone it locally following these instructions
- Note - If you are cloning my project, then you can skip all pip installs below and just run the following command in the terminal to install all the required libraries/packages at once:
pip install -r requirements.txt
- IMPORTANT - If developing locally on your device, ensure you set up/activate the virtual environment (see below) before installing/generating the requirements.txt file; failure to do this will pollute your machine and put other projects at risk
- Note - If you are cloning my project, then you can skip all pip installs below and just run the following command in the terminal to install all the required libraries/packages at once:
- Create a virtual environment on your machine (Can be skipped if using gitpod):
- python -m venv .venv
- To ensure the virtual environment is not tracked by version control, add .venv to the .gitignore file.
- Install Django with version 3.2:
pip install django3.2
- Install gunicorn:
pip install gunicorn
- Install supporting libraries:
pip install dj_database_url psycopg2
pip install dj3-cloudinary-storage
- Create requirements.txt:
pip freeze --local > requirements.txt
- Create an empty folder for your project in your chosen location.
- Create a project in the above folder:
- django-admin startproject <PROJECT_NAME> (in the case of this project, the project name was "jobsagooden")
- Create an app within the project:
python manage.py startapp APP_NAME
(in the case of this project, the app name was "job_search")
- Add a new app to the list of installed apps in setting.py
- Migrate changes:
python manage.py migrate
- Test server works locally:
python manage.py runserver
(You should see the default Django success page)
The below works on the assumption that you already have an account with Heroku and are already signed in.
- Create a new Heroku app:
- Click "New" in the top right-hand corner of the landing page, then click "Create new app."
- Give the app a unique name:
- Will form part of the URL (in the case of this project, I called the Heroku app jobs-a-gooden)
- Select the nearest location:
- For me, this was Europe.
- Add Database to the Heroku app:
- Navigate to the Resources tab of the app dashboard. Under the heading "Add ons," search for "Heroku Postgres" and click on it when it appears.
- Select "Hobby Dev - Free" from the "plan name" drop-down menu and click "Submit Order Form."
- From your editor, go to your projects settings.py file and copy the SECRET_KEY variable. Add this to the same name variable under the Heroku App's config vars.
- left box under config vars (variable KEY) = SECRET_KEY
- right box under config vars (variable VALUE) = Value copied from settings.py in project.
- Install dotenv package:
- pip install python-dotenv
- On your local machine, create a file called ".env" at the same level as settings.py and add this to the .gitignore file.
- From the Heroku app settings tab, click "reveal config vars" and copy the value of the variable DATABASE_URL. Add this value to a variable called DATABASE_URL in your create .env file:
DATABASE_URL=PastedValueFromHerokuHere
- (note that with the dotenv package no quotation marks are required)
- From your projects settings.py file, copy the SECRET_KEY value and assign it to a variable called SECRET_KEY in your .env file
SECRET_KEY=PastedValueFromYourProjectsSettings.pyFile
- Add DEVELOPMENT variable to .env file:
DEVELOPMENT=development
- Add CLOUDINARY_URL variable to .env file:
- Log into cloudinary and from the dashboard copy the API Environmental Variable.
- Add to .env file like below
CLOUDINARY_URL=PastedApiEnvironmentalVariable
-
At the top of your settings.py file, add the following snippet immediately after the other imports:
import os import dj_database_url if os.path.isfile('jobsagooden/.env'): from dotenv import load_dotenv load_dotenv() SECRET_KEY = os.environ.get("SECRET_KEY") DEBUG = "DEVELOPMENT" in os.environ
-
Delete the value from the setting.py DATABASES section and replace it with the following snippet to link up the Heroku Postgres server:
# Database
if development: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } else: import dj_database_url
DATABASES = {
'default': dj_database_url.parse(os.environ.get("DATABASE_URL")),
}
```
-
Add Cloudinary libraries to the installed apps section of settings.py file:
INSTALLED_APPS = [ …, 'cloudinary_storage', 'django.contrib.staticfiles', 'cloudinary', …, ] (note: order is important)
-
Tell Django to use Cloudinary to store media and static files by placing this snippet under the comments indicated below:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'cloudinary_storage.storage.StaticHashedCloudinaryStorage'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static/')]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = '/media/'
DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage'
-
Under the line with BASE_DIR, link templates directly in Heroku via settings.py:
TEMPLATES_DIR = os.path.join(BASE_DIR, 'templates')
-
Within TEMPLATES array, add
'DIRS':[TEMPLATES_DIR]
like the below example:
TEMPLATES = [
{
…,
'DIRS': [TEMPLATES_DIR],
…,
},
},
]
-
Add allowed hosts to settings.py:
ALLOWED_HOSTS = ["PROJECT_NAME.herokuapp.com", "localhost"]
-
Create Procfile at the top level of the file structure and insert the following:
web: gunicorn PROJECT_NAME.wsgi
-
Make an initial commit and push the code to the GitHub Repository.
git add .
git commit -m "Initial deployment"
git push
-
Click on Account Settings (under the avatar menu)
-
Scroll down to the API Key section and click Reveal. Copy the API key.
-
Log in to Heroku via the console and enter your details.
- heroku login-i
- When prompted, enter your Heroku username
- Enter copied API key as the password
-
Get your app name from Heroku
heroku apps
-
Set Heroku remote
heroku git:remote -a <app_name>
-
Add, Commit, Pust to GitHub:
git add . && git commit -m "Deploy to Heroku via CLI"
-
Push to GitHub and Heroku
git push origin main
git push heroku main
-
Navigate to the GitHub repository, and follow these steps to clone the project into your IDE of choice.
- Gitpod only requires you to have the web extension installed and click the green Gitpod button from the repositories main page. If you are using Gitpod, please skip step 2 below as you do not require a virtual environment to protect your machine.
-
Create the virtual environment with the terminal command
python3 -m venv venv
. Once complete add the "venv" file to you're ".gitignore" file and use the terminal commandvenv\Scripts\activate.bat
to activate it.- IMPORTANT If developing locally on your device, ensure you set up/activate the virtual environment before installing/generating the requirements.txt file; failure to do this will pollute your machine and put other projects at risk.
-
Install the requirements listed in requirements.txt using the terminal command
pip3 install -r requirements.txt
- Kindly note that since I developed the project from scratch and installed the required libraries as I progressed I have already included a requirements.txt for this app by using the terminal command
pip3 freeze > requirements.txt
to generate it.
- Kindly note that since I developed the project from scratch and installed the required libraries as I progressed I have already included a requirements.txt for this app by using the terminal command
-
Create your own Heroku app, and update allowed hosts in settings.py.
-
Run server locally with
python mange.py runserver