Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Error: No DjangoTemplates backend is configured

Tags:

python

django

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.
like image 581
Gustavo Rangel Avatar asked May 07 '17 17:05

Gustavo Rangel


People also ask

What is Django backend backend?

TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { # ... some options here ... }, }, ] BACKEND is a dotted Python path to a template engine class implementing Django’s template backend API.

What is a template in Django?

A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. A template is rendered with a context.

How to debug missing template variables in Django?

Unfortunately, it can be quite hard to debug such mistakes, since Django’s default behaviour is to ignore the problem and render an empty string. In this post we’ll look at several techniques to check for missing template variables: Using the string_if_invalid option - the built-in option, but a bit cumbersome.

What is %Django and how do I use it?

Django will replace the %s with the name of the missing variable, which can help track down the problem. Setting the opiton unconditionally like this is not recommended, since it can appear in production as well.


1 Answers

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'})
like image 114
Garrett Kadillak Avatar answered Oct 09 '22 19:10

Garrett Kadillak