1

I've installed Django 1.9.7, and I have Pythons 3.4.3 and 2.7.10 on Ubuntu.

These are the steps I've followed:

  1. Made a new project with django-admin startproject testproject
  2. cd testproject/testproject
  3. Made an app within the project with django-admin startapp testapp
  4. Made a directory for templates in that app with mkdir testapp/templates and added a very basic index.html template in there
  5. Edited settings.py to change the template backend to django.template.backends.jinja2.Jinja2, by editing line 57 of the default settings file, and to add testproject.testapp to INSTALLED_APPS; the TEMPLATES section is therefore like this:

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.jinja2.Jinja2',
            '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',
                ],
            },
        },
    ]
    
  6. Edited urls.py, adding from testproject.testapp import views and a URL pattern url(r'^$', views.index),

  7. Edited testapp/views.py adding

    def index(request):
        return render(request, 'index.html')
    
  8. cd ..

  9. Ran the server with python3 manage.py runserver, or python manage.py runserver -- very similar effect
  10. Take a browser to http://localhost:3000

I get an error. On the command line I get this:

Internal Server Error: /
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/django/template/utils.py", line 86, in __getitem__
    return self._engines[alias]
KeyError: 'jinja2'

Followed by another exception caused "during handling of the above exception", which matches the exception I see in the browser:

Environment:


Request Method: GET
Request URL: http://localhost:3000/

Django Version: 1.9.7
Python Version: 3.4.3
Installed Applications:
['django.contrib.staticfiles', 'testproject.testapp', 'webpack_loader']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/usr/local/lib/python3.4/dist-packages/django/template/utils.py" in __getitem__
  86.             return self._engines[alias]

During handling of the above exception ('jinja2'), another exception occurred:

File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py" in get_response
  174.                     response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py" in get_response
  172.                     response = response.render()

File "/usr/local/lib/python3.4/dist-packages/django/template/response.py" in render
  160.             self.content = self.rendered_content

File "/usr/local/lib/python3.4/dist-packages/django/template/response.py" in rendered_content
  135.         template = self._resolve_template(self.template_name)

File "/usr/local/lib/python3.4/dist-packages/django/template/response.py" in _resolve_template
  90.         new_template = self.resolve_template(template)

File "/usr/local/lib/python3.4/dist-packages/django/template/response.py" in resolve_template
  80.             return select_template(template, using=self.using)

File "/usr/local/lib/python3.4/dist-packages/django/template/loader.py" in select_template
  55.     engines = _engine_list(using)

File "/usr/local/lib/python3.4/dist-packages/django/template/loader.py" in _engine_list
  143.     return engines.all() if using is None else [engines[using]]

File "/usr/local/lib/python3.4/dist-packages/django/template/utils.py" in all
  110.         return [self[alias] for alias in self]

File "/usr/local/lib/python3.4/dist-packages/django/template/utils.py" in <listcomp>
  110.         return [self[alias] for alias in self]

File "/usr/local/lib/python3.4/dist-packages/django/template/utils.py" in __getitem__
  101.             engine = engine_cls(params)

File "/usr/local/lib/python3.4/dist-packages/django/template/backends/jinja2.py" in __init__
  35.         self.env = environment_cls(**options)

Exception Type: TypeError at /
Exception Value: __init__() got an unexpected keyword argument 'context_processors'

I get very similar traces with Python 2.

I found this question which has a similar error message (KeyError: 'jinja2') but seems to be a separate problem, and this bug report which has the same error again whose solution is to install jinja2, but jinja2 is definitely installed. At least, I can run python or python3 and then import jinja2. pip says jinja2 is up to date.

I must be missing something crucial -- any ideas?

Community
  • 1
  • 1
tremby
  • 9,541
  • 4
  • 55
  • 74
  • 1
    It sounds like you have changed the default template backend from django to jinja - it would be better to add Jinja as an additional backend (see [this answer](http://stackoverflow.com/questions/30701631/how-to-use-jinja2-as-a-templating-engine-in-django-1-8). If you replace the Django template backend, then apps that use Django templates won't work any more, including the admin. – Alasdair Jun 08 '16 at 22:45
  • 1
    Also, Django will look for Jinja templates in `testapp/jinja2`, not `testapp/templates`. – Alasdair Jun 08 '16 at 22:47
  • `mv testproject/testapp/templates testproject/testapp/jinja2` didn't change anything (I think it's not even getting as far as trying to find the specified template), but that's useful to know; thanks. I then rolled back the template backend settings to stock and then added a new entry for Jinja2 as you suggested, but omitted the options key from the linked answer completely (since I don't yet know what Environment does) and left DIRS empty (since APP_DIRS will do what I want), and it now works. If you write this an an answer I'll accept it. Thanks! – tremby Jun 08 '16 at 22:53
  • I was specific in the question that it was all default other than what I changed. `context_processors` was there by default -- I hadn't touched it since I didn't know what it was. The documentation isn't very clear when it comes to Jinja2, and what I understood from it was that all I had to do was change the `BACKEND`. – tremby Jun 09 '16 at 00:28
  • Bu,the documentation for Jinja2 is [very comprehensive](https://docs.djangoproject.com/en/1.9/topics/templates/#django.template.backends.jinja2.Jinja2), showing exactly what options are allowed; context_processors is not one of them. – Daniel Roseman Jun 09 '16 at 06:32
  • That's true. I confess I hadn't looked to the Jinja2 docs, only to the Django ones. – tremby Jun 09 '16 at 18:47
  • I feel the downvote is unfair. Would the downvoter remove that if I include the (mis)configuration which was asked for in a deleted comment? – tremby Jun 09 '16 at 18:49
  • I've made that edit. – tremby Jun 09 '16 at 18:59

1 Answers1

5

The error about context_processors is because the Jinja2 backend does not support that argument.

You should add an additional backend to your TEMPLATES setting, rather than replacing the existing backend from django to jinja2. See this answer for more information. If you replace the existing backend, then apps that require Django templates will not work, including the admin.

Finally, the jinja2 backend will look for templates in testapp/jinja2, not testapp/templates.

Community
  • 1
  • 1
Alasdair
  • 298,606
  • 55
  • 578
  • 516