# ♻️ 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:

```python
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:

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

---

### ✅ In Templates

Use **template inheritance**:

```html
<!-- base.html -->
<!DOCTYPE html>
<html>
<head>{% block head %}{% endblock %}</head>
<body>
  {% block content %}{% endblock %}
</body>
</html>
```

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

---

## 🔐 2. Environment Variables

Don't hardcode secrets like this:

```python
SECRET_KEY = 'abc123'  ❌
```

Use a `.env` file:

```bash
# .env
SECRET_KEY=mysecretkey
DEBUG=True
```

Load it using `python-decouple` or `django-environ`.

### Install:

```bash
pip install python-decouple
```

### Update [`settings.py`](http://settings.py):

```python
from decouple import config

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

---

## 🗂️ 3. Git & GitHub Setup

### ✅ Initialize Git

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

### ✅ Push to GitHub

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

---

## 🛑 4. Add `.gitignore`

Create `.gitignore` in your root directory:

```txt
# .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`

```css
# .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
    

---
