From 6b876a6f3f305b1fb55674030aadaff2db50c0ce Mon Sep 17 00:00:00 2001 From: Fabian Binz Date: Fri, 6 Dec 2024 12:22:51 +0100 Subject: [PATCH] Don't rely on existence of BASE_DIR settings variable. --- README.md | 4 ++++ django_cotton/cotton_loader.py | 11 ++++++++--- .../docs_project/templates/configuration.html | 13 ++++++++++++- .../docs_project/templates/quickstart.html | 5 ++++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 60b1ab6..d87fd1a 100644 --- a/README.md +++ b/README.md @@ -474,6 +474,10 @@ In addition, Cotton enables you to navigate around some of the limitations with The directory where your components are stored. +`COTTON_BASE_DIR` (default: None) + +The directory that contains your project-level "templates" directory. + `COTTON_SNAKE_CASED_NAMES` (default: True) Whether to search for component filenames in snake_case. If set to False, you can use kebab-cased / hyphenated filenames. diff --git a/django_cotton/cotton_loader.py b/django_cotton/cotton_loader.py index ef550af..e3a7ca6 100755 --- a/django_cotton/cotton_loader.py +++ b/django_cotton/cotton_loader.py @@ -61,9 +61,14 @@ def get_dirs(self): dirs.append(template_dir) # Check project root templates, e.g. project/templates - root_template_dir = os.path.join(settings.BASE_DIR, "templates") - if os.path.isdir(root_template_dir): - dirs.append(root_template_dir) + base_dir = getattr(settings, "COTTON_BASE_DIR", None) + if base_dir is None: + base_dir = getattr(settings, "BASE_DIR", None) + + if base_dir is not None: + root_template_dir = os.path.join(base_dir, "templates") + if os.path.isdir(root_template_dir): + dirs.append(root_template_dir) return dirs diff --git a/docs/docs_project/docs_project/templates/configuration.html b/docs/docs_project/docs_project/templates/configuration.html index 6f096a4..e49565c 100644 --- a/docs/docs_project/docs_project/templates/configuration.html +++ b/docs/docs_project/docs_project/templates/configuration.html @@ -13,6 +13,17 @@

Configuration

+
+
+ COTTON_BASE_DIR +
str (default: None)
+
+
+ The base directory where - in addition to the app folders - cotton will search for the "templates" directory (see above). + If not set, the `BASE_DIR` generated by `django-admin startproject` is used as a fallback, if it exists. +
+
+
@@ -42,4 +53,4 @@
As False
- \ No newline at end of file + diff --git a/docs/docs_project/docs_project/templates/quickstart.html b/docs/docs_project/docs_project/templates/quickstart.html index dd40394..b905492 100644 --- a/docs/docs_project/docs_project/templates/quickstart.html +++ b/docs/docs_project/docs_project/templates/quickstart.html @@ -81,7 +81,10 @@

Templates location

  • App level - You can place your cotton folder in any of your installed app folders, like:
    [project]/[app]/templates/cotton/row.html
  • -
  • Project root - You can place your cotton folder in a project level templates directory, like:
    [project]/templates/cotton/row.html
  • +
  • + Project root - You can place your cotton folder in a project level templates directory, like:
    [project]/templates/cotton/row.html
    + (where the [project] depends on the `BASE_DIR` or `COTTON_BASE_DIR` settings) +
  • Any style will allow you to include your component the same way: {{ ''|force_escape }}