Django
Beginner
1 min read
PostgreSQL, Environment Variables, and CI/CD
Example
# .env (never commit this file)
DJANGO_SECRET_KEY=your-very-long-random-secret-key-here
DJANGO_SETTINGS_MODULE=mysite.settings.production
ALLOWED_HOSTS=example.com,www.example.com
DB_NAME=mysite_db
DB_USER=mysite_user
DB_PASSWORD=strongpassword123
DB_HOST=localhost
DB_PORT=5432
REDIS_URL=redis://127.0.0.1:6379/1
# requirements.txt (production)
# Django==5.0.4
# gunicorn==21.2.0
# psycopg2==2.9.9
# django-environ==0.11.2
# djangorestframework==3.15.1
# Pillow==10.3.0
# .github/workflows/deploy.yml — GitHub Actions CI/CD
# name: Deploy
# on:
# push:
# branches: [main]
# jobs:
# deploy:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - name: Run tests
# run: |
# pip install -r requirements.txt
# python manage.py test --settings=mysite.settings.test
# - name: Deploy to server
# uses: appleboy/ssh-action@v1
# with:
# host: ${{ secrets.SERVER_HOST }}
# username: deploy
# key: ${{ secrets.SSH_PRIVATE_KEY }}
# script: |
# cd /var/www/mysite
# git pull origin main
# source venv/bin/activate
# pip install -r requirements.txt
# python manage.py collectstatic --noinput
# python manage.py migrate
# sudo systemctl restart gunicorn
Related Resources
Django Reference
Complete tag & property list
Django How-To Guides
Step-by-step practical guides
Django Exercises
Practice what you've learned
More in Django
This is the last lesson in this section.
Create a free account to earn a certificate