πŸš€ Best Practices for Structuring a Django Project

Azeem Akhtar
3 min readJan 3, 2025

--

πŸš€ Best Practices for Structuring a Django Project

Are you struggling to organize your Django projects? πŸ˜“ Let’s make your life easier! In this article, I’ll introduce you to a Django boilerplate πŸ› οΈ that follows industrial standards βœ…. Say goodbye to messy structures and hello to building amazing features faster! ✨

πŸ‘‰ Check out my boilerplate on Linkedin

🌟 Why Use This Boilerplate?

  • Saves time πŸ•’
  • Simplifies project management πŸ“‚
  • Impresses peers and collaborators πŸŽ‰ (yes, my students love it!)

In this guide, we’ll cover:
1️⃣ Setting up a Virtual Environment 🌐
2️⃣ Creating a Django Project πŸ“¦
3️⃣ Managing Dependencies πŸ“œ
4️⃣ Optimizing Settings βš™οΈ
5️⃣ Structuring Django Apps πŸ—οΈ
6️⃣ Using Makefile for Command Shortcuts πŸ–±οΈ

1️⃣ Start with a Virtual Environment (VE)

Why use a virtual environment? 🌱

  • Keeps dependencies isolated πŸ”’
  • Avoids polluting the global package list 🧹
  • Reduces the risk of breaking other projects πŸ’₯

πŸ› οΈ Steps to Create a VE

python3 -m venv .venv --prompt django-first

Activate it:

source .venv/bin/activate

Install Django:

pip install Django

Why is this important? A virtual environment ensures your Django project is clean, secure, and manageable. 🧼

2️⃣ Creating Your Django Project

Run this command to create a new Django project without unnecessary nested folders:

django-admin startproject config .

🌟 Best Practice

Instead of this messy structure:

project
β”œβ”€β”€ project
β”‚ β”œβ”€β”€ settings.py
β”‚ └── urls.py

Use this streamlined version:

project  
β”œβ”€β”€ config
β”‚ β”œβ”€β”€ settings.py
β”‚ └── urls.py

Clean, right? 😍

3️⃣ Manage Dependencies Like a Pro πŸ€“

Create separate requirements files for different environments (e.g., development, production).

Example structure:

requirements/  
β”œβ”€β”€ base.txt
β”œβ”€β”€ dev.txt

πŸ› οΈ Example Content:

base.txt:

Django~=5.0  
django-environ==0.11.2

dev.txt:

-r base.txt  
ipython==8.18.1
django-extensions==3.2.3

πŸ“Œ Tip: Use -r base.txt in environment-specific files to avoid duplications!

4️⃣ Optimizing Settings for Scalability βš™οΈ

Split your settings into multiple files:

  • base.py: Common settings πŸ› οΈ
  • dev.py: Development-specific settings 🌱

Example structure:

config/  
β”œβ”€β”€ settings/
β”‚ β”œβ”€β”€ base.py
β”‚ └── dev.py

This modular approach keeps your project organized and scalable. πŸ“ˆ

5️⃣ Organize Django Apps Like a Boss πŸ—οΈ

Instead of cluttering your root folder, group your apps under an apps/ directory.

Run:

mkdir apps  
cd apps
python3 ../manage.py startapp <app-name>

πŸ’‘ Core App Tip

Create reusable code for shared functionality (e.g., timestamps for models).

Example:

# apps/core/abstracts/models.py  
class CreatedModifiedAbstract(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)

class Meta:
abstract = True

6️⃣ Simplify Commands with Makefile πŸ–±οΈ

Tired of typing long commands? Use a Makefile to create shortcuts!

Example Makefile:

dev-start:  
python3 manage.py runserver --settings=config.settings.dev

dev-migrate:
python3 manage.py migrate --settings=config.settings.dev

To run your server:

make dev-start

🏁 Conclusion

The Django framework is powerful, but structuring your project poorly can slow you down. 😬 By using this boilerplate and best practices, you’ll:

  • Save time ⏳
  • Build scalable applications πŸ’‘
  • Impress your peers πŸ†

So, why wait? Start building your next Django masterpiece today! πŸš€

Got questions? Drop them in the comments! πŸ’¬

--

--

Azeem Akhtar
Azeem Akhtar

Written by Azeem Akhtar

Python, Machine Learning, Deep Learning, Data Science, Django, Artificial Intelligence

Responses (3)