Deploying to Heroku - potatoscript/django GitHub Wiki
🌐 Deploying Django to Heroku / PythonAnywhere
Once you've built your Django application, the next step is deploying it so users can access it. Two popular platforms for deploying Django projects are Heroku and PythonAnywhere. These platforms make it easy to deploy web apps with minimal setup.
In this tutorial, we’ll walk through how to deploy a Django application to both Heroku and PythonAnywhere. We’ll start with Heroku, which is great for beginners and free-tier users, and then show how to deploy on PythonAnywhere.
🧩 Part 1: Deploying Django to Heroku
Heroku is a cloud platform that allows you to deploy and manage applications easily. It provides an intuitive interface for deploying apps, scaling them, and managing your database.
🛠️ Step 1: Preparing Your Django Project for Heroku
-
Install Required Packages
You need a few dependencies to deploy to Heroku. These include:
gunicorn
: A production-ready web server.psycopg2
: PostgreSQL database adapter (since Heroku uses PostgreSQL).dj-database-url
: A utility to parse theDATABASE_URL
environment variable.whitenoise
: A package to serve static files in production.
To install these, run the following:
pip install gunicorn psycopg2 dj-database-url whitenoise
-
Update
settings.py
for ProductionOpen your
settings.py
file and make the following changes:-
Database Configuration: Use
dj-database-url
to configure your database from the environment variableDATABASE_URL
.import dj_database_url DATABASES = { 'default': dj_database_url.config(default='postgres://localhost') }
-
Static Files Handling: Set up
whitenoise
to serve static files.MIDDLEWARE = [ # Add 'whitenoise' middleware to serve static files 'whitenoise.middleware.WhiteNoiseMiddleware', # other middlewares... ] STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
-
Allowed Hosts: Update the
ALLOWED_HOSTS
setting.ALLOWED_HOSTS = ['your-heroku-app.herokuapp.com']
-
Debug Mode: Make sure
DEBUG
is set toFalse
in production.DEBUG = False
-
-
Create a
Procfile
The
Procfile
tells Heroku how to run your application. Create aProcfile
in the root directory of your project:web: gunicorn your_project_name.wsgi
-
Set Up Static Files
Run the following to collect all static files in a single location for serving:
python manage.py collectstatic
-
Requirements File
Make sure all your dependencies are listed in
requirements.txt
. Run:pip freeze > requirements.txt
🛠️ Step 2: Deploying to Heroku
Now that the app is ready, let’s deploy it to Heroku.
-
Create a Heroku Account: If you haven’t already, create an account at heroku.com.
-
Install Heroku CLI: Download and install the Heroku Command Line Interface (CLI) from Heroku's website.
-
Login to Heroku: In your terminal, run:
heroku login
This will open a browser window where you can log in to your Heroku account.
-
Create a New Heroku App: Run the following command to create a new app:
heroku create your-app-name
-
Initialize a Git Repository: If you haven’t already, initialize a Git repository for your Django project:
git init git add . git commit -m "Initial commit"
-
Add Heroku Remote: Heroku automatically adds a remote called
heroku
to your Git repository. You can check it with:git remote -v
-
Push to Heroku: Deploy your project to Heroku by pushing it to the Heroku remote:
git push heroku master
-
Run Database Migrations: After deployment, you’ll need to run migrations to set up your database:
heroku run python manage.py migrate
-
Open Your App: Your app should now be live on Heroku! Open it by running:
heroku open
🧩 Part 2: Deploying Django to PythonAnywhere
PythonAnywhere is a cloud hosting platform specifically designed for Python applications. It’s simple to use and is a great option for deploying Django apps with minimal setup.
🛠️ Step 1: Prepare Your Django Project
Before deploying to PythonAnywhere, follow these steps to prepare your Django project:
-
Install Required Packages: Similar to Heroku, you need
gunicorn
for serving your app in production. Install it via:pip install gunicorn
-
Static Files: Make sure your Django app is set up to handle static files, similar to the setup for Heroku. You’ll need to configure
whitenoise
to serve the static files.-
Static files configuration in
settings.py
:STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Run
collectstatic
to gather your static files:python manage.py collectstatic
-
-
Requirements File: Make sure your dependencies are up-to-date in
requirements.txt
:pip freeze > requirements.txt
🛠️ Step 2: Deploying to PythonAnywhere
-
Create a PythonAnywhere Account: If you don’t have one, create an account at pythonanywhere.com.
-
Upload Your Django Project: You can upload your Django project to PythonAnywhere using the file manager or by cloning it from a Git repository. For Git, run:
git clone your-repository-url
-
Set Up a Virtual Environment: On PythonAnywhere, it’s a good practice to use a virtual environment. You can create one using:
python3.8 -m venv myenv
Activate the virtual environment:
source myenv/bin/activate
-
Install Dependencies: After activating the virtual environment, install the necessary dependencies:
pip install -r requirements.txt
-
Configure Web App: In PythonAnywhere, go to the Web tab and click Add a new web app. Select Django and follow the instructions. You will need to specify:
- The path to your Django project.
- The virtual environment you created.
-
Database Setup: If you’re using a database, you can either use SQLite or configure PostgreSQL by following the PythonAnywhere database instructions.
-
Static Files: Make sure PythonAnywhere knows where to find your static files. Set the path for static files and media files in the Web app configuration.
🧩 Step 3: Run the Django Application
Once everything is set up, visit your PythonAnywhere domain (e.g., yourusername.pythonanywhere.com
). Your app should now be live!
📝 Conclusion
Both Heroku and PythonAnywhere are excellent platforms for deploying Django applications. While Heroku is better suited for fast deployment and scalability, PythonAnywhere offers an easy-to-use interface for smaller projects and personal apps.
Whether you're deploying to Heroku or PythonAnywhere, the basic steps are similar: prepare your app for production, install necessary packages, configure static files, and push your code to the platform. Once deployed, you’ll be able to showcase your Django application to the world! 🚀