How to Organize Django Project: Best Practices and Structure
To organize a Django project, create a main project folder containing a settings module and separate apps for each feature using
django-admin startproject and python manage.py startapp. Keep settings modular, use clear app responsibilities, and separate static and templates folders for clean structure.Syntax
A Django project is organized into a main project folder and multiple apps. Use these commands:
django-admin startproject projectnamecreates the main project folder with settings and URLs.python manage.py startapp appnamecreates an app folder for a specific feature.
Inside the project folder, you have:
settings.py: configuration settings.urls.py: URL routing.wsgi.pyorasgi.py: server interfaces.
Each app has its own models, views, templates, and static files.
bash
django-admin startproject myproject cd myproject python manage.py startapp blog
Example
This example shows a simple Django project named myproject with an app called blog. The blog app handles posts and has its own models, views, and templates.
plaintext
myproject/
├── manage.py
├── myproject/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── asgi.py
│ └── wsgi.py
└── blog/
├── migrations/
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
├── views.py
├── templates/
│ └── blog/
│ └── post_list.html
└── static/
└── blog/
└── style.cssOutput
Folder structure created with separate project and app folders.
Common Pitfalls
Common mistakes when organizing Django projects include:
- Putting all code in one app instead of splitting by feature.
- Mixing static and template files without clear folders.
- Keeping all settings in one large file instead of modularizing for different environments.
- Not registering apps in
INSTALLED_APPScausing them not to work.
Always keep apps focused and settings clean.
python
## Wrong: Putting models of different features in one app # blog/models.py class Post(models.Model): title = models.CharField(max_length=100) class Comment(models.Model): post = models.ForeignKey(Post, on_delete=models.CASCADE) text = models.TextField() ## Right: Separate apps for posts and comments # posts/models.py class Post(models.Model): title = models.CharField(max_length=100) # comments/models.py class Comment(models.Model): post = models.ForeignKey('posts.Post', on_delete=models.CASCADE) text = models.TextField()
Quick Reference
- Main project folder: Contains settings, URLs, and server files.
- Apps: Each app handles a specific feature or domain.
- Templates and static: Organized inside each app or in global folders.
- Settings: Modularize for dev, test, and production.
- Register apps: Add each app to
INSTALLED_APPSin settings.
Key Takeaways
Organize Django projects by creating separate apps for each feature.
Keep settings modular and apps registered in INSTALLED_APPS.
Use clear folders for templates and static files inside apps.
Avoid mixing unrelated code in one app to keep code maintainable.
Follow Django’s default project structure for easy scaling.