Group your Django apps in a directory

The way you structure your Django project is important for maintainability, specially when the number of apps are growing and they get mixed with other directories such as the media and static development dirs, any development tool folders, the scripting one...

When I have more than 3/4 apps in a project I use to create a common folder for them and group them as follows:

apps/
  β”œβ”€β”€ app1/
  β”‚   β”œβ”€β”€ migrations
  β”‚   β”œβ”€β”€ __init__.py
  β”‚   β”œβ”€β”€ apps.py
  β”‚   β”œβ”€β”€ models.py
  β”‚   β”œβ”€β”€ views.py
  β”‚   └── tests.py
  β”œβ”€β”€ app2/
  β”œβ”€β”€ app3/
  β”œβ”€β”€ app4/
  └── __init__.py

The advantage of doing that is it makes it easier to browse to a app's code. At least for me.

When doing that there some stuff to consider.

1. First

Make sure you create the __init__.py, so that the apps folder is treated as a python module.

2. Second thing

The app label for django needs to be specified like in the snippet below:

# apps/app1/apps.py
# or apps/app1/__init__.py

class App1Config(AppConfig):
    name = "apps.app1"

Note that you can actually create your AppConfig subclass under your app's __init__.py file

3. Last

As you guessed it the installed app setting needs to be a little different

# config/settings.py

INSTALLED_APPS = [
    ...
    # My apps
    "apps.app1.apps.App1Config", # or apps.app1
    "apps.app2.apps.App2Config", # or apps.app2
    "apps.app3.apps.App3Config", # or apps.app3
    "apps.app4.apps.App4Config", # or apps.app4
    ...
]

I have never wrote many time the word apps!


πŸ“Š Create a modern CV
Create a modern CV

Create a modern CV to impress recruiters