1

I get an error when I try to start my server. I'm using Django in a virtual environment. I've spent hours searching for this & can't find an answer

Here is the error that I received:

Traceback (most recent call last):
File "C:\Users\jaiso\.virtualenvs\storefront2-4TwSyq5h\lib\site-packages\django\template\utils.py", line 69, in __getitem__
    return self._engines[alias]
KeyError: 'django'

Here are my installed packages:

Package Version
--------------------- -------
asgiref 3.5.2
Django 4.1.2
django-debug-toolbar 3.7.0
django-rest-framework 0.1.0
djangorestframework 3.14.0
drf-nested-routers 0.93.4
mysqlclient 2.1.1
pip 22.2.2
Python-Rest-Framework 0.3.14
pytz 2022.4
setuptools 65.3.0
six 1.16.0
sqlparse 0.4.3
tzdata 2022.4
wheel 0.37.1

4
  • It's most likely coming from the TEMPLATES in the settings. #1 make sure it's there and #2 if you've recently upgrade/have an old settings try and grab a newer version. Commented Oct 13, 2022 at 1:26
  • Thanks Nealium but I haven't modified my Templates setting since beginning my project & it was working a couple of days ago. I uninstalled & reinstalled Django as well as Django Rest Framework. I even used old version of both to see if it was caused by a new version. That didn't work either Commented Oct 13, 2022 at 2:28
  • Are you sure that your Virtualenv is activated? And your packages are installed inside? Commented Oct 13, 2022 at 11:16
  • @claudius Yes, the installed packages list is what's installed in the virtual environment. When I check the PIP version, its showing the path to the virtual env. PS G:\My Folder\Projects\Python Stuff\djangoProject\storefront2> pip -V pip 22.2.2 from C:\Users\jaiso\.virtualenvs\storefront2-4TwSyq5h\lib\site-packages\pip (python 3.9) Commented Oct 13, 2022 at 15:06

1 Answer 1

1

So I assume you aren't doing custom template stuff, so here's most likely the fix.

Go into your settings.py and make sure your TEMPLATES look like this:

  • Take specific Note on the BACKEND value
TEMPLATES = [
    {
        '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',
            ],
        },
    },
]

I changed the BACKEND Value to: template.backends.django.DjangoTemplates and I got the same error (removed the first django.)

Traceback (most recent call last):
  File "C:\Users\{me}\Envs\tmpdjango\lib\site-packages\django\template\utils.py", line 71, in __getitem__
    self.run(**options)
    return self._engines[alias]
KeyError: 'django'

and it's due to:

# django.templates.util. ~26
def templates(self):
    if self._templates is None:
        self._templates = settings.TEMPLATES

    # stuff chopped out


    for tpl in self._templates:
        try:
            # ********** This Line \/ *******************
            default_name = tpl["BACKEND"].rsplit(".", 2)[-2]

            # 'django.template.backends.django.DjangoTemplates'.rsplit(".", 2)[-2]
            # out: 'django'

        except Exception:
            pass # (actually an error here)

    ## futher down
    tpl = {
        "NAME": default_name,
        "DIRS": [],
        "APP_DIRS": False,
        "OPTIONS": {},
        **tpl,
    }

    templates[tpl["NAME"]] = tpl

    # more Stuff

and where the error is happening: alias seems to always be django (not sure why)

# django.templates.util. ~69
def __getitem__(self, alias):

    print('alias',alias)
    # alias == 'django'

    try:
        return self._engines[alias]
    except KeyError:
        try:
            params = self.templates[alias]
            # print(self.templates)
        except KeyError:
            raise InvalidTemplateEngineError(
                "Could not find config for '{}' "
                "in settings.TEMPLATES".format(alias)
            )
Sign up to request clarification or add additional context in comments.

9 Comments

Here are my settings for Templates: TEMPLATES = [ { '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', ], }, }, ]
When I remove "django" @ the beginning of my Backend setting, I get the error ModuleNotFoundError: No module named 'template'
@jwill Yeah I also got that 'No module' error, but the other error is further up. Mmmmmm.. I can't recreate it otherwise.. Can you go to: C:\Users\jaiso\.virtualenvs\storefront2-4TwSyq5h\lib\site-packages\django\template\utils.py and place print(self.templates) right above params = self.templates[alias] (line 69) and give me the output?
Yes, there's a try except wrapped around return self._engines[alias]. When I try to print self._engines, I get 2 empty dictionaries. I guess that's why its not finding the alias (django). I don't know how to resolve this.
It looks like this may be a cause as well. File "C:\Users\*\django\template\backends\django.py", line 130, in get_package_libraries File "C:\Users\*\django\core\management\commands\runserver.py", line 118, in run raise InvalidTemplateLibrary( django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'rest_framework.templatetags.rest_framework': cannot import name 'SkipError' from 'rest_framework.exceptions' (C:\Users\jaiso\.virtualenvs\storefront2-4TwSyq5h\lib\site-packages\rest_framework\exceptions.py)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.