diff --git a/dev/concepts/fundamentals/autodiscovery/index.html b/dev/concepts/fundamentals/autodiscovery/index.html index b5339ebf..a0c728cc 100644 --- a/dev/concepts/fundamentals/autodiscovery/index.html +++ b/dev/concepts/fundamentals/autodiscovery/index.html @@ -3,7 +3,7 @@ @register("calendar") class Calendar(Component): ... -
But for the component to be registered, the code needs to be executed - the file needs to be imported as a module.
One way to do that is by importing all your components in apps.py
:
But for the component to be registered, the code needs to be executed - and for that, the file needs to be imported as a module.
One way to do that is by importing all your components in apps.py
:
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = "my_app"
@@ -14,10 +14,10 @@
from components.menu.menu import Menu
from components.button.button import Button
...
-
However, there's a simpler way!
By default, the Python files in the COMPONENTS.dirs
directories (and app-level [app]/components/
) are auto-imported in order to auto-register the components.
Autodiscovery occurs when Django is loaded, during the AppConfig.ready
hook of the apps.py
file.
If you are using autodiscovery, keep a few points in mind:
components
dir, that you would not want to run anyway.@register
p.py
, and module name should follow PEP-8.Autodiscovery can be disabled in the settings.
Autodiscovery can be also triggered manually, using the autodiscover
function. This is useful if you want to run autodiscovery at a custom point of the lifecycle:
However, there's a simpler way!
By default, the Python files in the COMPONENTS.dirs
directories (and app-level [app]/components/
) are auto-imported in order to auto-register the components.
Autodiscovery occurs when Django is loaded, during the AppConfig.ready
hook of the apps.py
file.
If you are using autodiscovery, keep a few points in mind:
components
dir, that you would not want to run anyway.@register
.py
, and module name should follow PEP-8._
(except __init__.py
) are ignored.Autodiscovery can be disabled in the settings.
Autodiscovery can be also triggered manually, using the autodiscover
function. This is useful if you want to run autodiscovery at a custom point of the lifecycle:
To get the same list of modules that autodiscover()
would return, but without importing them, use get_component_files()
: