tremby
tremby

Reputation: 10069

Unable to set up Jinja2 with Django

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?

Upvotes: 1

Views: 2699

Answers (1)

Alasdair
Alasdair

Reputation: 308849

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.

Upvotes: 5

Related Questions