Deploying a Django project means making your web application live on the internet so users can access it.
After building your project locally, deployment allows it to run on a production server.
Development vs Production
Development:
- Runs on local machine
- Uses built-in Django server
- Used for testing
Production:
- Hosted on a live server
- Uses production web server
- Accessible worldwide
You should never use runserver for production.
Step 1: Prepare Your Project for Deployment
1. Set DEBUG to False
In settings.py:
DEBUG = False
2. Add Allowed Hosts
ALLOWED_HOSTS = ['yourdomain.com', 'www.yourdomain.com']
This prevents security issues.
3. Configure Static Files
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Then collect static files:
python manage.py collectstatic
Step 2: Choose a Hosting Platform
Popular platforms for deploying Django:
- Heroku
- PythonAnywhere
- DigitalOcean
- AWS (Amazon Web Services)
- Render
- VPS Hosting
Choose based on budget and project size.
Step 3: Use Production Server (Gunicorn)
Install Gunicorn:
pip install gunicorn
Run project using Gunicorn:
gunicorn myproject.wsgi
Gunicorn acts as a production web server for Django.
Step 4: Use Nginx (Optional but Recommended)
Nginx is used to:
- Handle static files
- Manage traffic
- Improve performance
- Add security
Production architecture:
User → Nginx → Gunicorn → Django → Database
Step 5: Configure Database
In production, use:
- PostgreSQL (recommended)
- MySQL
Avoid using SQLite for large production systems.
Example PostgreSQL configuration:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'dbname',
'USER': 'dbuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432',
}
}
Step 6: Environment Variables
Do not hard-code sensitive data.
Use environment variables for:
- SECRET_KEY
- Database credentials
- API keys
Example:
import os
SECRET_KEY = os.environ.get('SECRET_KEY')
Step 7: Migrate Database on Server
After deployment:
python manage.py migrate
Create superuser:
python manage.py createsuperuser
Step 8: Enable HTTPS
Use SSL certificate for secure connection.
HTTPS:
- Encrypts data
- Protects users
- Required for production apps
Let’s Encrypt provides free SSL certificates.
Common Deployment Files
requirements.txt → List of project dependencies
Procfile → Used in Heroku
.env → Environment variables file
Create requirements file:
pip freeze > requirements.txt
Deployment Checklist
Set DEBUG = False
Add ALLOWED_HOSTS
Configure database
Configure static files
Use Gunicorn
Use Nginx
Use environment variables
Enable HTTPS
Why Deployment Knowledge is Important
Deployment helps you:
Launch real-world applications
Share projects with clients
Build production-ready systems
Understand server management
Work as a professional developer
Key Takeaway
Deploying a Django project involves configuring production settings, choosing a hosting platform, setting up a production server like Gunicorn, managing static files, securing data, and connecting a production database.
Proper deployment ensures your application is secure, scalable, and accessible to users worldwide.