Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.8 with Jinja2: Contrib app Admin not working

I upgraded to a fresh install of Django 1.8 and began using Jinja2 as it said that it was supported now and Jinja2 has some features I could use in my project.

After finishing adapting the templates for my app to Jinja2 and taking advantage of the new features now available, I discovered that the contrib app Admin no longer works.

"TemplateDoesNotExist at /admin/login/"

So it turns out that contrib app Admin only has templates made for DjangoTemplates and not for Jinja2. I did the naive thing first and made a symlink in [...]/site-packages/django/contrib/admin from templates to jinja2 but the templates were using DjangoTemplates specifics and so Jinja2 would not accept them. I removed the symlink.

The way I have switched over to Jinja2 is by making the following change in my project settings.py:

 TEMPLATES = [
     {
-        'BACKEND': 'django.template.backends.django.DjangoTemplates',
+        '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',
-            ],
-        },
     },
 ]

Does anyone know of either:

  1. How to let the contrib app Admin keep using DjangoTemplates while letting my own app use Jinja2, or
  2. If there is another admin app as good as the default one which supports Jinja2, or
  3. If there is something else I have overlooked or should be aware of?

Thank you for your time :)

like image 909
Grim Fandango Avatar asked Apr 02 '15 18:04

Grim Fandango


2 Answers

The admin app does not come with Jinja2 templates. You need to configure your project to use Django and Jinja2 templates.

The Django template docs has the following example.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            '/home/html/example.com',
            '/home/html/default',
        ],
    },
    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2',
        'DIRS': [
            '/home/html/jinja2',
        ],
    },
]
like image 130
Alasdair Avatar answered Sep 22 '22 06:09

Alasdair


The ideal solution would be to make the default template system take precedence over the Jinja system. The only difference is the DIRS specified need to be different. If you are only concerned about the admin, the process is fairly easy.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [], # This is particularly important as it will not look into the default template directory
    },
    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
    },
]

Just remember that the directory structure must not allow the default template system to access your main pages.

like image 26
Srivats Shankar Avatar answered Sep 22 '22 06:09

Srivats Shankar