DEV Community

Cover image for My Preconfigured Django Template: Part 4 - Python-Decouple
Matúš Kočik
Matúš Kočik

Posted on • Originally published at Medium

My Preconfigured Django Template: Part 4 - Python-Decouple

What is Python-Decouple?

Python-Decouple is a simple yet powerful tool that helps manage Django configuration settings securely. Instead of hardcoding secrets like SECRET_KEY or database credentials in settings.py, it allows you to store them in environment variables or a .env file, keeping your codebase clean and safe.

Why Use Python-Decouple?

  • Better security – Keeps sensitive data out of your code.
  • Easy environment switching – Quickly adapt configurations for development, staging, and production.
  • Less clutter in settings.py – Keeps settings organized and modular.
  • Automatic type conversion – Easily manage strings, integers, and lists.
  • Seamless integration – Works with Django, CI/CD pipelines, and containerized deployments.

Quick Commands

# Install Python-Decouple

uv add python-decouple
Enter fullscreen mode Exit fullscreen mode
# Create a .env file and store secrets

SECRET_KEY=mysecretkey
DEBUG=True
DATABASE_NAME=mydatabase
DATABASE_USER=myuser
DATABASE_PASSWORD=mypassword
Enter fullscreen mode Exit fullscreen mode
# Use Python-Decouple in settings.py

from decouple import config

SECRET_KEY = config("SECRET_KEY")
DEBUG = config("DEBUG", default=False, cast=bool)
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": config("DATABASE_NAME"),
        "USER": config("DATABASE_USER"),
        "PASSWORD": config("DATABASE_PASSWORD"),
        "HOST": "localhost",
        "PORT": 5432,
    }
}
Enter fullscreen mode Exit fullscreen mode

📖 Read the full article on Medium


🔗 Follow me on:

🌍 My Website

💻 GitHub

🤝 LinkedIn

✍️ Medium

Top comments (0)