The Best Practices for Organizing Your Django Project Structure

A well-organized project structure makes it easier to work on your code, collaborate with others, and maintain your project in the long term. While Django provides a default structure when you start a new project, you’ll often need to make adjustments to better suit your needs.

In this blog, we’ll look at some best practices for organizing your Django project structure effectively.


1. Use a Clear Directory Structure

Django’s default structure is fine for small projects, but as your application grows, it’s a good idea to organize your files in a way that scales. A common structure looks like this:

my_project/
    manage.py
    requirements.txt
    config/
        __init__.py
        settings.py
        urls.py
        wsgi.py
        asgi.py
    apps/
        app1/
            migrations/
            __init__.py
            admin.py
            apps.py
            models.py
            tests.py
            views.py
        app2/
            ...
    static/
    templates/
    media/

Key Points:

  • Group all settings, URLs, and WSGI/ASGI configurations in a config/ directory.
  • Keep apps within an apps/ directory for better organization.
  • Separate static files, templates, and media into their respective top-level folders.

2. Split Settings into Multiple Files

A single settings.py file can become unwieldy as your project grows. Splitting it into multiple files helps keep things manageable.

A common pattern is to create a settings/ directory:

config/settings/
    __init__.py
    base.py
    development.py
    production.py

How It Works:

  • base.py contains common settings.
  • development.py and production.py extend base.py with environment-specific settings.

In __init__.py, import the appropriate settings based on an environment variable:

import os
from .base import *

env = os.getenv('DJANGO_ENV', 'development')
if env == 'production':
    from .production import *
else:
    from .development import *

3. Create Reusable Apps

Django encourages a modular approach, so it’s a good idea to make your apps reusable. Each app should focus on a single purpose.

For example:

  • users/ for user authentication and profiles.
  • blog/ for managing blog posts.
  • ecommerce/ for handling store-related functionality.

Tip: If you think an app might be useful in another project, keep its code clean and free from project-specific dependencies.


4. Use a Consistent Naming Convention

Use meaningful and consistent names for files and directories. Stick to Django’s naming conventions for models, views, and templates:

  • Models: Singular (e.g., User, Product)
  • Templates: Match the app and view (e.g., blog/post_list.html)
  • Static Files: Organize by app (e.g., static/blog/style.css)

5. Keep Business Logic Out of Views

Django’s views should focus on handling HTTP requests and responses. Complex business logic should be moved to services, utils, or models.

Example structure:

apps/blog/
    services.py
    utils.py
    models.py
    views.py

Why It Matters: This keeps your views lean and makes your code easier to test and maintain.


6. Use a Requirements File

Track your project’s dependencies using a requirements.txt file or Pipfile. This ensures everyone working on the project uses the same library versions.

Generate a requirements.txt file:

pip freeze > requirements.txt

7. Configure Static and Media Files Properly

Use dedicated directories for static files and user-uploaded media. For production, use a cloud-based solution like AWS S3 for better scalability.

Project structure:

static/
    css/
    js/
    images/
media/
    uploads/

Configure your settings:

STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static']

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

8. Use Version Control

Always use a version control system like Git. Exclude sensitive files and unnecessary directories using a .gitignore file. Here’s an example for Django:

*.pyc
__pycache__/
.env
/static/
/media/

9. Document Your Project

Add a README.md file with basic instructions on setting up and running the project. If your project is more complex, include additional documentation in a docs/ directory.


10. Regularly Review and Refactor

As your project grows, revisit your structure to ensure it still meets your needs. Refactor code that has become outdated or redundant.


Conclusion

A well-organized project structure can save you a lot of headaches down the line. By following these best practices, you can make your Django project easier to work with, whether you’re working solo or with a team.


TAGS

d j a n g o