Repository: cookiecutter/cookiecutter-django

Issue:
django-debug-toolbar 3.2.3 introduced APPS_DIR set to True warning
## What happened?


https://github.com/cookiecutter/cookiecutter-django/pull/3466 bumped django-debug-toolbar to version 3.2.3. This version introducted the following WARNING into Django, which persisted in 3.2.4:

````
django      | System check identified some issues:
django      |
django      | WARNINGS:
django      | ?: (debug_toolbar.W006) At least one DjangoTemplates TEMPLATES configuration needs to have APP_DIRS set to True.
django      |   HINT: Use APP_DIRS=True for at least one django.template.backends.django.DjangoTemplates backend configuration.
````

## What should've happened instead?

No warning should have been shown.

## Additional details

<!-- To assist you best, please include commands that you've run, options you've selected and any relevant logs -->

The APPS_DIR situation also surfaces when implementing some packages like django-sitemaps, that  also tells that it is necessary to have at least one APPS_DIR=True somewhere in the templating options ([check the docs](https://docs.djangoproject.com/en/4.0/ref/contrib/sitemaps/#installation)).

cookiecutter-django's current approach seems to accomplish similar results by using template-dir by template-dir approarch instead of the APPS_DIR=True catchall. That might be a good thing, but somehow at least in two instances (one part of cookiecutter-django and one that seems to be part of the Django Project itself) are complaining about this.

So the question is: is there a workaround that warning, and a way to circumvent django-sitemaps instructions and keep the current templating approach? And is there any downside to having it instead of the APPS_DIR=True solution?


* Host system configuration:
    * Version of cookiecutter: Cookiecutter 1.7.3
    * OS name and version: docker
    * Python version: 3.10
    * Docker version: 20.10.10
    * docker-compose version: 1.29.2
    * cookiecutter-django version: **2021.12.15**
    *  django-debug-toolbar: **3.2.4**




Discussion:
Thanks for the report. That's too bad we didn't see it in tests, I see the warning is there. To avoid this to happen in the future, we should probably include a call to `python manage.py check --fail-level WARNING` as part of our CI.

No idea why we don't have `APPS_DIR=True` in the project...

> somehow at least in two instances (one part of cookiecutter-django and one that seems to be part of the Django Project itself) are complaining about this.

Could you expand on that? Is it that 2 places aren't acceptiong the catchall setting?
When faced with django-sitemaps instructions, I added the APPS_DIR=True in settings/base-py under TEMPLATES = []. Django will quit on a config error.

It is incompatible with the "loaders": [] way that cookiecutter-django does it. They are mutually excludent somehow.

There are some warnings that are thrown because of docker images running root that might break CI as well. That might be tricky.

I am still trying to figure out the difference between using the loaders: [] way and the APPS_DIR way of searching for templates.

The interesting part is that django-sitemaps seems to work fine with the loaders: [] approach as well, contrary to the docs.
Ok, so I looked at this more closely. Setting `APP_DIR=True` is equivalent to [adding the `app_directories.Loader`](https://docs.djangoproject.com/en/dev/ref/templates/api/#django.template.loaders.app_directories.Loader):

> django.template.loaders.app_directories.Loader
> ...
> You can enable this loader by setting `APP_DIRS` to `True`

Looks like our settings locally are the same as if we were just setting `APP_DIRS` to `True` since the other one we use is enabled by default.

We don't set `APP_DIRS` to `True` because our prod setup is a bit more optimised. We alter the default config to wrap our loaders with [`cached.Loader`](https://docs.djangoproject.com/en/dev/ref/templates/api/#django.template.loaders.cached.Loader): 

https://github.com/cookiecutter/cookiecutter-django/blob/ce1c76e34e60395b3d012f9037e138651159459e/%7B%7Bcookiecutter.project_slug%7D%7D/config/settings/production.py#L129-L137

Now, looking at the documentation again, I see this note (that was [changed in Django 1.11](https://docs.djangoproject.com/en/4.0/releases/1.11/#miscellaneous)):

> This loader is automatically enabled if `OPTIONS['loaders']` isn’t specified and `OPTIONS['debug']` is `False` (the latter option defaults to the value of `DEBUG`).

If I'm reading all this correctly, it looks like our setup can be simplified as follows:

- Add `'APP_DIRS': True`
- Don't specificy the `loaders` option (we use the default values)
- Remove the above lines from `production.py` where we chenge the `loaders` option in `TEMPLATES` 

I'm not touching templates setting very often, hopefully I've read the docs correctly... Am I missing anything?

Resolving patch:
diff --git a/{{cookiecutter.project_slug}}/config/settings/base.py b/{{cookiecutter.project_slug}}/config/settings/base.py
--- a/{{cookiecutter.project_slug}}/config/settings/base.py
+++ b/{{cookiecutter.project_slug}}/config/settings/base.py
@@ -182,15 +182,11 @@
     {
         # https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND
         "BACKEND": "django.template.backends.django.DjangoTemplates",
-        # https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs
+        # https://docs.djangoproject.com/en/dev/ref/settings/#dirs
         "DIRS": [str(APPS_DIR / "templates")],
+        # https://docs.djangoproject.com/en/dev/ref/settings/#app-dirs
+        "APP_DIRS": True,
         "OPTIONS": {
-            # https://docs.djangoproject.com/en/dev/ref/settings/#template-loaders
-            # https://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types
-            "loaders": [
-                "django.template.loaders.filesystem.Loader",
-                "django.template.loaders.app_directories.Loader",
-            ],
             # https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
             "context_processors": [
                 "django.template.context_processors.debug",
diff --git a/{{cookiecutter.project_slug}}/config/settings/production.py b/{{cookiecutter.project_slug}}/config/settings/production.py
--- a/{{cookiecutter.project_slug}}/config/settings/production.py
+++ b/{{cookiecutter.project_slug}}/config/settings/production.py
@@ -123,19 +123,6 @@
 MEDIA_URL = f"https://storage.googleapis.com/{GS_BUCKET_NAME}/media/"
 {%- endif %}
 
-# TEMPLATES
-# ------------------------------------------------------------------------------
-# https://docs.djangoproject.com/en/dev/ref/settings/#templates
-TEMPLATES[-1]["OPTIONS"]["loaders"] = [  # type: ignore[index] # noqa F405
-    (
-        "django.template.loaders.cached.Loader",
-        [
-            "django.template.loaders.filesystem.Loader",
-            "django.template.loaders.app_directories.Loader",
-        ],
-    )
-]
-
 # EMAIL
 # ------------------------------------------------------------------------------
 # https://docs.djangoproject.com/en/dev/ref/settings/#default-from-email


Test patch:
diff --git a/tests/test_bare.sh b/tests/test_bare.sh
--- a/tests/test_bare.sh
+++ b/tests/test_bare.sh
@@ -29,6 +29,9 @@ pre-commit run --show-diff-on-failure -a
 # run the project's tests
 pytest
 
+# Make sure the check doesn't raise any warnings
+python manage.py check --fail-level WARNING
+
 if [ -f "package.json" ]
 then
     npm install
diff --git a/tests/test_docker.sh b/tests/test_docker.sh
--- a/tests/test_docker.sh
+++ b/tests/test_docker.sh
@@ -35,3 +35,6 @@ docker-compose -f local.yml run django python manage.py makemigrations --dry-run
 
 # Test support for translations
 docker-compose -f local.yml run django python manage.py makemessages --all
+
+# Make sure the check doesn't raise any warnings
+docker-compose -f local.yml run django python manage.py check --fail-level WARNING
diff --git a/{{cookiecutter.project_slug}}/config/settings/test.py b/{{cookiecutter.project_slug}}/config/settings/test.py
--- a/{{cookiecutter.project_slug}}/config/settings/test.py
+++ b/{{cookiecutter.project_slug}}/config/settings/test.py
@@ -20,18 +20,6 @@
 # https://docs.djangoproject.com/en/dev/ref/settings/#password-hashers
 PASSWORD_HASHERS = ["django.contrib.auth.hashers.MD5PasswordHasher"]
 
-# TEMPLATES
-# ------------------------------------------------------------------------------
-TEMPLATES[-1]["OPTIONS"]["loaders"] = [  # type: ignore[index] # noqa F405
-    (
-        "django.template.loaders.cached.Loader",
-        [
-            "django.template.loaders.filesystem.Loader",
-            "django.template.loaders.app_directories.Loader",
-        ],
-    )
-]
-
 # EMAIL
 # ------------------------------------------------------------------------------
 # https://docs.djangoproject.com/en/dev/ref/settings/#email-backend