Tuesday 30 October 2018

Ordering Django applications

Why the order of INSTALLED_APPS matters in Django

When you create a Django project, the INSTALLED_APPS setting will be generated for you like this:

# Somewhere in settings.py...

Then, when you create an app, or install a library, you’re faced with a choice: do you add the new item above or below Django’s apps?

Well, it’s good practice to add new apps above Django’s, unless you have a specific reason not to. I’ve seen this done the other way around a lot, so it might not be what you expect.

This is because when Django is looking for something, it will go through the INSTALLED_APPS list, from the top, one app at a time, until it has found what it’s looking for. Putting your code first means it takes priority.

For example, if you want to override a template in Django’s admin, you can do so by placing a modified version of the original into the templates/ directory of one of your apps.

This applies to:

It’s also handy to know that this is the order in which apps will be registered, and admin integration discovered.

Finally: don’t add the core app’s templates/ directory into TEMPLATES[...]['DIRS'] (or TEMPLATE_DIRS)! Instead, simply add each app containing templates/ to INSTALLED_APPS.


In a Django projects, expect to see INSTALLED_APPS structured like this:

    # The "core" (AKA "project" or "main") app.

    # Other apps in the project.

    # Libraries.

    # Django.
    ...  # Etc.

This ensures: