refactor: Fix template caching, expose cached_template, Component.template API changes (#647)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Juro Oravec 2024-09-06 22:40:39 +02:00 committed by GitHub
parent 589e802625
commit 841dd77e91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 347 additions and 56 deletions

View file

@ -3,10 +3,14 @@ from django_components import Component, register
@register("calendar")
class Calendar(Component):
# Note that Django will look for templates inside `[your apps]/components` dir and
# `[project root]/components` dir. To customize which template to use based on context
# you can override def get_template_name() instead of specifying the below variable.
# Templates inside `[your apps]/components` dir and `[project root]/components` dir
# will be automatically found.
#
# `template_name` can be relative to dir where `calendar.py` is, or relative to STATICFILES_DIRS
template_name = "calendar/calendar.html"
# Or
# def get_template_name(context):
# return f"template-{context['name']}.html"
# This component takes one parameter, a date string to show in the template
def get_context_data(self, date):
@ -27,10 +31,14 @@ class Calendar(Component):
@register("calendar_relative")
class CalendarRelative(Component):
# Note that Django will look for templates inside `[your apps]/components` dir and
# `[project root]/components` dir. To customize which template to use based on context
# you can override def get_template_name() instead of specifying the below variable.
# Templates inside `[your apps]/components` dir and `[project root]/components` dir
# will be automatically found.
#
# `template_name` can be relative to dir where `calendar.py` is, or relative to STATICFILES_DIRS
template_name = "calendar.html"
# Or
# def get_template_name(context):
# return f"template-{context['name']}.html"
# This component takes one parameter, a date string to show in the template
def get_context_data(self, date):

View file

@ -3,10 +3,14 @@ from django_components import Component, register
@register("calendar_nested")
class CalendarNested(Component):
# Note that Django will look for templates inside `[your apps]/components` dir and
# `[project root]/components` dir. To customize which template to use based on context
# you can override def get_template_name() instead of specifying the below variable.
# Templates inside `[your apps]/components` dir and `[project root]/components` dir
# will be automatically found.
#
# `template_name` can be relative to dir where `calendar.py` is, or relative to STATICFILES_DIRS
template_name = "calendar.html"
# Or
# def get_template_name(context):
# return f"template-{context['name']}.html"
# This component takes one parameter, a date string to show in the template
def get_context_data(self, date):

View file

@ -2,8 +2,7 @@ from django_components import Component, register
@register("todo")
class Calendar(Component):
# Note that Django will look for templates inside `[your apps]/components` dir and
# `[project root]/components` dir. To customize which template to use based on context
# you can override def get_template_name() instead of specifying the below variable.
class Todo(Component):
# Templates inside `[your apps]/components` dir and `[project root]/components` dir
# will be automatically found.
template_name = "todo/todo.html"