mirror of
https://github.com/django-components/django-components.git
synced 2025-08-31 03:07:19 +00:00
docs: document how to access component instance from within View (#1115)
This commit is contained in:
parent
5b01d6c3c6
commit
d37291a3b6
2 changed files with 36 additions and 0 deletions
|
@ -81,6 +81,30 @@ class Calendar(Component):
|
|||
|
||||
This is deprecated from v0.137 onwards, and will be removed in v1.0.
|
||||
|
||||
### Acccessing component instance
|
||||
|
||||
You can access the component instance from within the View methods by using the [`View.component`](../../../reference/api#django_components.ComponentView.component) attribute:
|
||||
|
||||
```py
|
||||
class Calendar(Component):
|
||||
...
|
||||
|
||||
class View:
|
||||
def get(self, request):
|
||||
return self.component.render_to_response(request=request)
|
||||
```
|
||||
|
||||
!!! note
|
||||
|
||||
The [`View.component`](../../../reference/api#django_components.ComponentView.component) instance is a dummy instance created solely for the View methods.
|
||||
|
||||
It is the same as if you instantiated the component class directly:
|
||||
|
||||
```py
|
||||
component = Calendar()
|
||||
component.render_to_response(request=request)
|
||||
```
|
||||
|
||||
## Register URLs manually
|
||||
|
||||
To register the component as a route / endpoint in Django, add an entry to your
|
||||
|
|
|
@ -33,6 +33,18 @@ class ComponentView(ComponentExtension.ExtensionClass, View): # type: ignore
|
|||
# NOTE: This attribute must be declared on the class for `View.as_view()` to allow
|
||||
# us to pass `component` kwarg.
|
||||
component = cast("Component", None)
|
||||
"""
|
||||
The component instance.
|
||||
|
||||
This is a dummy instance created solely for the View methods.
|
||||
|
||||
It is the same as if you instantiated the component class directly:
|
||||
|
||||
```py
|
||||
component = Calendar()
|
||||
component.render_to_response(request=request)
|
||||
```
|
||||
"""
|
||||
|
||||
def __init__(self, component: "Component", **kwargs: Any) -> None:
|
||||
ComponentExtension.ExtensionClass.__init__(self, component)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue