♻️ Day 8: Reusability & Best Practices – Beginner’s Web Dev Series

Welcome to Day 8 of our Django journey!

By now, you’ve built working views, forms, authentication flows, and templates. Today, you’ll learn how to keep your code clean, reusable, and production-ready using the DRY principle, environment variables, and Git for version control.


📍 What You’ll Learn

  1. The DRY Principle in Django (Don’t Repeat Yourself)

  2. How to use reusable models, views, and templates

  3. Why and how to use environment variables

  4. How to set up Git and GitHub

  5. Adding a .gitignore file

  6. Creating and using .env and .env.example


🔁 1. The DRY Principle in Django

DRY = Don't Repeat Yourself
Reusability = Avoid writing the same thing multiple times.

✅ In Models

Instead of repeating timestamp fields in every model:

class BaseTimestampModel(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

Then reuse:

class Post(BaseTimestampModel):
    title = models.CharField(max_length=200)

✅ In Templates

Use template inheritance:

<!-- base.html -->
<!DOCTYPE html>
<html>
<head>{% block head %}{% endblock %}</head>
<body>
  {% block content %}{% endblock %}
</body>
</html>
<!-- home.html -->
{% extends 'base.html' %}
{% block content %}
  <h1>Welcome!</h1>
{% endblock %}

🔐 2. Environment Variables

Don't hardcode secrets like this:

SECRET_KEY = 'abc123'

Use a .env file:

# .env
SECRET_KEY=mysecretkey
DEBUG=True

Load it using python-decouple or django-environ.

Install:

pip install python-decouple

Update settings.py:

from decouple import config

SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', cast=bool)

🗂️ 3. Git & GitHub Setup

✅ Initialize Git

git init
git add .
git commit -m "Initial commit"

✅ Push to GitHub

gh repo create myproject --public
git push -u origin main

🛑 4. Add .gitignore

Create .gitignore in your root directory:

# .gitignore
*.pyc
__pycache__/
env/
venv/
db.sqlite3
*.env
/static/
/media/

✅ This prevents sensitive and unnecessary files from being tracked by Git.


📂 5. Create .env.example

# .env.example
DEBUG=True
SECRET_KEY=your-secret-key-here

✅ Helps teammates or future you understand what env vars are needed.


✅ Task for Day 8

  • Refactor a model using mixins (e.g., timestamps)

  • Setup a .env file using python-decouple

  • Add .gitignore and push project to GitHub

  • Create a .env.example file for sharing


🚀 Coming Up: Day 9 – REST API

In Day 9, we’ll cover:

  • Creating serializers

  • Creating API Views

  • Managing json payloads


0
Subscribe to my newsletter

Read articles from Shankar Lamichhane directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shankar Lamichhane
Shankar Lamichhane

Hi, I’m Shankar — a Sr. Software Engineer specializing in Python, Django, and DevOps. I build scalable web applications, APIs, and cloud-native systems, with a focus on clean architecture and backend automation.