cookiecutter / cookiecutter-django

Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.
https://cookiecutter-django.readthedocs.io
BSD 3-Clause "New" or "Revised" License
12.13k stars 2.9k forks source link

django-debug-toolbar 3.2.3 introduced APPS_DIR set to True warning #3475

Closed mpoli closed 2 years ago

mpoli commented 2 years ago

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

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).

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?

browniebroke commented 2 years ago

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?

mpoli commented 2 years ago

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.

browniebroke commented 2 years ago

Ok, so I looked at this more closely. Setting APP_DIR=True is equivalent to adding the 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://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):

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:

I'm not touching templates setting very often, hopefully I've read the docs correctly... Am I missing anything?