docs: update section on working with request object (#1126)

* docs: update section on working with request object

* refactor: fix linting
This commit is contained in:
Juro Oravec 2025-04-14 11:35:48 +02:00 committed by GitHub
parent 06cad2ec64
commit fc026cbd99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 83 additions and 45 deletions

View file

@ -751,10 +751,48 @@ class Component(
@property
def input(self) -> ComponentInput[ArgsType, KwargsType, SlotsType]:
"""
Input holds the data (like arg, kwargs, slots) that were passed to
the current execution of the `render` method.
Input holds the data that were passed to the current component at render time.
Raises `RuntimeError` if accessed outside of rendering execution.
This includes:
- [`args`](../api/#django_components.ComponentInput.args) - List of positional arguments
- [`kwargs`](../api/#django_components.ComponentInput.kwargs) - Dictionary of keyword arguments
- [`slots`](../api/#django_components.ComponentInput.slots) - Dictionary of slots. Values are normalized to
[`Slot`](../api/#django_components.Slot) instances
- [`context`](../api/#django_components.ComponentInput.context) -
[`Context`](https://docs.djangoproject.com/en/5.2/ref/templates/api/#django.template.Context)
object that should be used to render the component
- And other kwargs passed to [`Component.render()`](../api/#django_components.Component.render)
like `type` and `render_dependencies`
Read more on [Component inputs](../../concepts/fundamentals/render_api/#component-inputs).
**Example:**
Use can use [`self.input.args`](../api/#django_components.ComponentInput.args)
and [`self.input.kwargs`](../api/#django_components.ComponentInput.kwargs)
to access the positional and keyword arguments passed to
[`Component.render()`](../api/#django_components.Component.render).
```python
class Table(Component):
def get_context_data(self, *args, **kwargs):
# Access component's inputs, slots and context
assert self.input.args == [123, "str"]
assert self.input.kwargs == {"variable": "test", "another": 1}
footer_slot = self.input.slots["footer"]
some_var = self.input.context["some_var"]
return {}
rendered = TestComponent.render(
kwargs={"variable": "test", "another": 1},
args=(123, "str"),
slots={"footer": "MY_SLOT"},
)
```
"""
if not len(self._metadata_stack):
raise RuntimeError(f"{self.name}: Tried to access Component input while outside of rendering execution")