π 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! π¬