M
M
mr_forlife2021-10-13 12:25:28
Django
mr_forlife, 2021-10-13 12:25:28

How to connect a new application correctly?

How to correctly connect a new application in Django?
In different sources I met two ways:
1) in the official documentation such an option is given

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'newapp.apps.NewappConfig',  # new app
]


2) in many examples this option is used
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'newapp',  # new app
]


What is the difference between these two ways?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2021-10-13
@mr_forlife

What's wrong with the official description? Here is my free translation:

To configure the application, create an apps.py module inside the application, then define a class here - an AppConfig instance.
When INSTALLED_APPS contains a dot-separated path to an application module, by default, if Django finds only one instance of the AppConfig class in the apps.py module, it uses that configuration for the application. This behavior can be disabled by setting AppConfig.default to False.
If the apps.py module contains more than one instance of the AppConfig class, Django will use the one that has AppConfig.default set to True.
If no AppConfig instances are found, the base AppConfig will be used.
Alternatively, INSTALLED_APPS may contain a dot-separated path to the class to specify it explicitly:
INSTALLED_APPS = [
    ...
    'polls.apps.PollsAppConfig',
    ...
]


Django itself uses the path to the application by default:
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
...

but not
INSTALLED_APPS = [
    'django.contrib.admin.apps.AdminConfig',
    'django.contrib.auth.apps.AuthConfig',
...

Therefore, if for some reason you do not need to specify the path to the AppConfig instance of the application explicitly, then you can limit yourself to only the path to the application.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question