Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importance of apps orders in INSTALLED_APPS

Tags:

python

django

Is the order of apps in INSTALLED_APPS important? I ask it because I have settings folder with two settings files: base.py and production.py and I put all my settings in base.py and then in production.py, I have:

from base import *  

and then I override some settings. Also in my base.py, I make INSTALLED_APPS a list, not a tuple. Because I want to remove some apps for production settings. In production.py I want to write:

NOT_USED_APPS = ['debut_toolbar', 'other_odd_app',] INSTALLED_APPS = list(set(INSTALLED_APPS) - set(NOT_USED_APPS)) 

In this case, the order of apps in INSTALLED_APPS is not like in base.py

like image 232
litwisha Avatar asked Aug 10 '15 17:08

litwisha


People also ask

What is the use of apps py in Django?

The apps.py convention merely allows Django to load them automatically when INSTALLED_APPS contains the path to an application module rather than the path to a configuration class.

What are templates in Django?

A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. A template is rendered with a context.


1 Answers

Yes, the order is quite important.

From Django official docs on INSTALLED_APPS settings:

When several applications provide different versions of the same resource (template, static file, management command, translation), the application listed first in INSTALLED_APPS has precedence.

Example-1 Templates:

django.template.loaders.app_directories.Loader

If this template loader is enabled in your DjangoTemplates backend in the TEMPLATES setting or if you have passed it as a loaders argument to Engine, then it loads templates from Django apps on the filesystem.

For each app in INSTALLED_APPS, the loader looks for a templates subdirectory. If the directory exists, Django will look for templates in there.

Lets say in my project, i have defined INSTALLED_APPS as:

INSTALLED_APPS = ('myproject.app1', 'myproject.app2') 

Now, i want to get the template some_template.html. Then get_template('some_template.html') will look for some_template.html in these directories, in this order:

/path/to/myproject/app1/templates/ # checks here first /path/to/myproject/app2/templates/ # Then checks here 

It will then use the one which it finds first.

Quoting from that section:

The order of INSTALLED_APPS is significant!

Example-2: Translations

Django applies the following algorithm for discovering translations:

  1. The directories listed in LOCALE_PATHS have the highest precedence, with the ones appearing first having higher precedence than the ones appearing later.
  2. Then, it looks for and uses if it exists a locale directory in each of the installed apps listed in INSTALLED_APPS. The ones appearing first have higher precedence than the ones appearing later.
  3. Finally, the Django-provided base translation in django/conf/locale is used as a fallback.

We can see that order is important here also.

Example-3 Management Commands:

From Django 1.7 release notes on management commands and order of INSTALLED_APPS:

When several applications provide management commands with the same name, Django loads the command from the application that comes first in INSTALLED_APPS. Previous versions loaded the command from the application that came last.

This brings discovery of management commands in line with other parts of Django that rely on the order of INSTALLED_APPS, such as static files, templates, and translations.

like image 146
Rahul Gupta Avatar answered Sep 25 '22 01:09

Rahul Gupta