refactor: Remove safer_staticfiles, replace STATICFILES_DIRS with COMPONENTS.dirs, support [app]/components (#652)

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-11 08:45:55 +02:00 committed by GitHub
parent 728b4ffad7
commit e1382d3ccd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
34 changed files with 1034 additions and 264 deletions

View file

@ -54,7 +54,7 @@ even for production environment.
Assuming that you're running the prod server with:
1. `DEBUG = False` setting
2. `"django_components.safer_staticfiles"` in the `INSTALLED_APPS`
2. `"django.contrib.staticfiles"` in the `INSTALLED_APPS`
Then Django will server only JS and CSS files under the `/static/` URL path.

View file

@ -6,7 +6,7 @@ class Calendar(Component):
# 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` can be relative to dir where `calendar.py` is, or relative to COMPONENTS.dirs
template_name = "calendar/calendar.html"
# Or
# def get_template_name(context):
@ -19,10 +19,11 @@ class Calendar(Component):
}
def get(self, request, *args, **kwargs):
context = {
"date": request.GET.get("date", ""),
}
return self.render_to_response(context)
return self.render_to_response(
kwargs={
"date": request.GET.get("date", ""),
},
)
class Media:
css = "calendar/calendar.css"
@ -34,7 +35,7 @@ class CalendarRelative(Component):
# 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` can be relative to dir where `calendar.py` is, or relative to COMPONENTS.dirs
template_name = "calendar.html"
# Or
# def get_template_name(context):

View file

@ -7,8 +7,12 @@ from django_components import Component, register, types
class Greeting(Component):
def get(self, request, *args, **kwargs):
slots = {"message": "Hello, world!"}
context = {"name": request.GET.get("name", "")}
return self.render_to_response(context=context, slots=slots)
return self.render_to_response(
slots=slots,
kwargs={
"name": request.GET.get("name", ""),
},
)
def get_context_data(self, name, *args, **kwargs) -> Dict[str, Any]:
return {"name": name}

View file

@ -6,7 +6,7 @@ class CalendarNested(Component):
# 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` can be relative to dir where `calendar.py` is, or relative to COMPONENTS.dirs
template_name = "calendar.html"
# Or
# def get_template_name(context):
@ -19,10 +19,11 @@ class CalendarNested(Component):
}
def get(self, request, *args, **kwargs):
context = {
"date": request.GET.get("date", ""),
}
return self.render_to_response(context)
return self.render_to_response(
kwargs={
"date": request.GET.get("date", ""),
},
)
class Media:
css = "calendar.css"

View file

@ -28,10 +28,8 @@ INSTALLED_APPS = [
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
# Replaced by django_components.safer_staticfiles as of v0.27:
# "django.contrib.staticfiles",
"django.contrib.staticfiles",
"django_components",
"django_components.safer_staticfiles",
"calendarapp",
]
# Application definition
@ -79,14 +77,24 @@ TEMPLATES = [
},
]
STATICFILES_FINDERS = [
# Default finders
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
# Django components
"django_components.finders.ComponentsFileSystemFinder",
]
WSGI_APPLICATION = "sampleproject.wsgi.application"
# COMPONENTS = {
# "autodiscover": True,
# "libraries": [],
# "template_cache_size": 128,
# "context_behavior": "isolated", # "django" | "isolated"
# }
COMPONENTS = {
# "autodiscover": True,
"dirs": [BASE_DIR / "components"],
# "app_dirs": ["components"],
# "libraries": [],
# "template_cache_size": 128,
# "context_behavior": "isolated", # "django" | "isolated"
}
# Database
@ -135,7 +143,6 @@ USE_TZ = True
# https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = "static/"
STATICFILES_DIRS = [BASE_DIR / "components"]
# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field