Django Error: No DjangoTemplates backend is configured

django, python

Solution

For Django > 1.7 your `settings.py` you should have some value for the `BACKEND` key in the `TEMPLATES` list. Basically you should have something like this in your settings

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            # ... some options here ...
        },
    },
]

From the docs:

`BACKEND` is a dotted Python path to a template engine class implementing Django’s template backend API. The built-in backends are `django.template.backends.django.DjangoTemplates` and `django.template.backends.jinja2.Jinja2`.

Link

EDIT: To load templates from the command line rather than a view using the `render` method, you have to do a little more work.

If you don't want to use a template from disk, you can use the following:

from django.conf import settings
settings.configure()
from django.template import Template, Context
Template('Hello, {{ name }}!').render(Context({'name': 'world'}))

However, if you want to load templates from disk, this requires more effort. Something like this will work:

import django
from django.conf import settings
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['/path/to/template'],
    }
]
settings.configure(TEMPLATES=TEMPLATES)
django.setup()
from django.template.loader import get_template
from django.template import Context
template = get_template('my_template.html')
template.render(Context({'name': 'world'})

Problem

I'm using Django and I need to dynamically generate an HTML file from a given template (lab.html): ``` from django.template import Template, Context from django.conf import settings settings.configure() f = qc.QFile('lab.html') f.open(qc.QFile.ReadOnly | qc.QFile.Text) stream = qc.QTextStream(f) template = stream.readAll() print(template) f.close() t = Template(template) c = Context({"result": "test"}) #result is the variable in the html file that I am trying to replace ``` However, I keep getting this odd error that I haven't found anywhere after some research. Any thoughts? ``` Traceback (most recent call last): File "/Users/gustavorangel/PycharmProjects/help/HelpUI.py", line 262, in filesSearch t = Template(template) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/template/base.py", line 184, in __init__ engine = Engine.get_default() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/template/engine.py", line 83, in get_default "No DjangoTemplates backend is configured.") django.core.exceptions.ImproperlyConfigured: No DjangoTemplates backend is configured. ```

Original source