megalus / django-google-sso

Easily add Google Authentication to Django Admin
https://megalus.github.io/django-google-sso/
MIT License
27 stars 18 forks source link

KeyError: 'NAME' when using SSO_USE_ALTERNATE_W003 #44

Open dqd opened 4 hours ago

dqd commented 4 hours ago

Hello and first of all, thank you for your great Django SSO plugins.

After I installed django_google_sso and django_github_sso together on the latest version of Django 5.1, I got W003 warnings. As described in the documentation, this is expected and I should use this setting to silence original Django check and run an alternate check:

SILENCED_SYSTEM_CHECKS = ["templates.W003"]
SSO_USE_ALTERNATE_W003 = True

However, when SSO_USE_ALTERNATE_W003 is set to True, the app crashes for me:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/django_github_sso/checks/warnings.py", line 29, in register_sso_check
    from django.core.checks.templates import (
ImportError: cannot import name 'check_for_template_tags_with_the_same_name' from 'django.core.checks.templates' (/usr/local/lib/python3.12/site-packages/django/core/checks/templates.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/threading.py", line 1075, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.12/threading.py", line 1012, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.12/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.12/site-packages/django/core/management/commands/runserver.py", line 134, in inner_run
    self.check(display_num_errors=True)
  File "/usr/local/lib/python3.12/site-packages/django/core/management/base.py", line 486, in check
    all_issues = checks.run_checks(
                 ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_github_sso/checks/warnings.py", line 57, in register_sso_check
    django_engine = DjangoTemplates(engine_params)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/template/backends/django.py", line 27, in __init__
    super().__init__(params)
  File "/usr/local/lib/python3.12/site-packages/django/template/backends/base.py", line 18, in __init__
    self.name = params.pop("NAME")
                ^^^^^^^^^^^^^^^^^^
KeyError: 'NAME'

It looks like a bug to me.

dqd commented 4 hours ago

The workaround is to add NAME to TEMPLATES setting, like here:

TEMPLATES = [
    {
        "NAME": "django",  # <-- added
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

According to the Django documentation, NAME is optional (it defaults to the name of the module defining the engine class), so I still think this is a bug.