refactor: Update docs and tests to use get_template_data() (#1161)

* refactor: update docs and tests to use get_template_data()

* refactor: fix linting

* docs: add note about difference between the two methods
This commit is contained in:
Juro Oravec 2025-05-03 12:04:10 +02:00 committed by GitHub
parent c69980493d
commit 28b61c1609
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
69 changed files with 795 additions and 725 deletions

View file

@ -1,27 +1,11 @@
import time
from typing import Any, Dict
from typing import NamedTuple
from django_components import Component, register, types
@register("recursive")
class Recursive(Component):
class View:
def get(self, request):
time_before = time.time()
output = Recursive.render_to_response(
request=request,
kwargs={
"depth": 0,
},
)
time_after = time.time()
print("TIME: ", time_after - time_before)
return output
def get_context_data(self, depth: int = 0) -> Dict[str, Any]:
return {"depth": depth + 1}
template: types.django_html = """
<div id="recursive">
depth: {{ depth }}
@ -31,3 +15,25 @@ class Recursive(Component):
{% endif %}
</div>
"""
class Kwargs(NamedTuple):
depth: int
class Defaults:
depth = 0
def get_template_data(self, args, kwargs: Kwargs, slots, context):
return {"depth": kwargs.depth + 1}
class View:
def get(self, request):
time_before = time.time()
output = Recursive.render_to_response(
request=request,
kwargs=Recursive.Kwargs(
depth=0,
),
)
time_after = time.time()
print("TIME: ", time_after - time_before)
return output