This example defines an extension with a command that prints "Hello world". To run the command, the user would run components ext run hello_world hello.
This example defines an extension with a command that prints "Hello world". To run the command, the user would run components ext run hello_world hello.
Name must be lowercase, and must be a valid Python identifier (e.g. "my_extension").
The extension may add new features to the Component class by allowing users to define and access a nested class in the Component class.
The extension name determines the name of the nested class in the Component class, and the attribute under which the extension will be accessible.
E.g. if the extension name is "my_extension", then the nested class in the Component class will be MyExtension, and the extension will be accessible as MyComp.my_extension.
Name must be lowercase, and must be a valid Python identifier (e.g. "my_extension").
The extension may add new features to the Component class by allowing users to define and access a nested class in the Component class.
The extension name determines the name of the nested class in the Component class, and the attribute under which the extension will be accessible.
E.g. if the extension name is "my_extension", then the nested class in the Component class will be MyExtension, and the extension will be accessible as MyComp.my_extension.
This hook is called after the Component class is fully defined but before it's registered.
Use this hook to perform any initialization or validation of the Component class.
Example:
fromdjango_componentsimportComponentExtension,OnComponentClassCreatedContextclassMyExtension(ComponentExtension):defon_component_class_created(self,ctx:OnComponentClassCreatedContext)->None:# Add a new attribute to the Component classctx.component_cls.my_attr="my_value"
This hook is called before the Component class is deleted from memory.
Use this hook to perform any cleanup related to the Component class.
Example:
fromdjango_componentsimportComponentExtension,OnComponentClassDeletedContextclassMyExtension(ComponentExtension):defon_component_class_deleted(self,ctx:OnComponentClassDeletedContext)->None:# Remove Component class from the extension's cache on deletionself.cache.pop(ctx.component_cls,None)
Use this hook to modify or validate the component's data before rendering.
Example:
fromdjango_componentsimportComponentExtension,OnComponentDataContextclassMyExtension(ComponentExtension):defon_component_data(self,ctx:OnComponentDataContext)->None:# Add extra template variable to all components when they are renderedctx.template_data["my_template_var"]="my_value"
This hook also allows to skip the rendering of a component altogether. If the hook returns a non-null value, this value will be used instead of rendering the component.
You can use this to implement a caching mechanism for components, or define components that will be rendered conditionally.
This hook also allows to skip the rendering of a component altogether. If the hook returns a non-null value, this value will be used instead of rendering the component.
You can use this to implement a caching mechanism for components, or define components that will be rendered conditionally.
Example:
fromdjango_componentsimportComponentExtension,OnComponentInputContextclassMyExtension(ComponentExtension):defon_component_input(self,ctx:OnComponentInputContext)->None:# Add extra kwarg to all components when they are renderedctx.kwargs["my_input"]="my_value"
This hook is called after a Component class is successfully registered.
Example:
fromdjango_componentsimportComponentExtension,OnComponentRegisteredContextclassMyExtension(ComponentExtension):defon_component_registered(self,ctx:OnComponentRegisteredContext)->None:print(f"Component {ctx.component_cls} registered to {ctx.registry} as '{ctx.name}'")
Called when a Component was rendered, including all its child components.
Use this hook to access or post-process the component's rendered output.
To modify the output, return a new string from this hook.
Example:
fromdjango_componentsimportComponentExtension,OnComponentRenderedContextclassMyExtension(ComponentExtension):defon_component_rendered(self,ctx:OnComponentRenderedContext)->Optional[str]:# Append a comment to the component's rendered outputreturnctx.result+"<!-- MyExtension comment -->"
This hook is called after a Component class is removed from the registry.
Example:
fromdjango_componentsimportComponentExtension,OnComponentUnregisteredContextclassMyExtension(ComponentExtension):defon_component_unregistered(self,ctx:OnComponentUnregisteredContext)->None:print(f"Component {ctx.component_cls} unregistered from {ctx.registry} as '{ctx.name}'")
This hook is called after a new ComponentRegistry instance is initialized.
Use this hook to perform any initialization needed for the registry.
Example:
fromdjango_componentsimportComponentExtension,OnRegistryCreatedContextclassMyExtension(ComponentExtension):defon_registry_created(self,ctx:OnRegistryCreatedContext)->None:# Add a new attribute to the registryctx.registry.my_attr="my_value"
The name of the component to create. This is a required argument.
Options:
-h, --help
show this help message and exit
--path PATH
The path to the component's directory. This is an optional argument. If not provided, the command will use the COMPONENTS.dirs setting from your Django settings.
--js JS
The name of the JavaScript file. This is an optional argument. The default value is script.js.
--css CSS
The name of the CSS file. This is an optional argument. The default value is style.css.
--template TEMPLATE
The name of the template file. This is an optional argument. The default value is template.html.
--force
This option allows you to overwrite existing files if they exist. This is an optional argument.
--verbose
This option allows the command to print additional information during component creation. This is an optional argument.
--dry-run
This option allows you to simulate component creation without actually creating any files. This is an optional argument. The default value is False.
The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used.
--pythonpath PYTHONPATH
A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".
The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used.
--pythonpath PYTHONPATH
A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".
The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used.
--pythonpath PYTHONPATH
A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".
The name of the component to create. This is a required argument.
Options:
-h, --help
show this help message and exit
--path PATH
The path to the component's directory. This is an optional argument. If not provided, the command will use the COMPONENTS.dirs setting from your Django settings.
--js JS
The name of the JavaScript file. This is an optional argument. The default value is script.js.
--css CSS
The name of the CSS file. This is an optional argument. The default value is style.css.
--template TEMPLATE
The name of the template file. This is an optional argument. The default value is template.html.
--force
This option allows you to overwrite existing files if they exist. This is an optional argument.
--verbose
This option allows the command to print additional information during component creation. This is an optional argument.
--dry-run
This option allows you to simulate component creation without actually creating any files. This is an optional argument. The default value is False.
The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used.
--pythonpath PYTHONPATH
A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".
\ No newline at end of file
diff --git a/dev/reference/extension_commands/index.html b/dev/reference/extension_commands/index.html
index 8f12541a..a60416fb 100644
--- a/dev/reference/extension_commands/index.html
+++ b/dev/reference/extension_commands/index.html
@@ -18,7 +18,7 @@
version:Optional[str]=None,deprecated:Optional[bool]=None,)
-
Title for the argument group in help output; by default “positional arguments” if description is provided, otherwise uses title for positional arguments.
Title for the argument group in help output; by default “positional arguments” if description is provided, otherwise uses title for positional arguments.
Usage information that will be displayed with sub-command help, by default the name of the program and any positional arguments before the subparser argument.
Usage information that will be displayed with sub-command help, by default the name of the program and any positional arguments before the subparser argument.