7

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.
Pang
  • 9,564
  • 146
  • 81
  • 122
Gustavo Rangel
  • 131
  • 1
  • 9

2 Answers2

11

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'})
Garrett Kadillak
  • 1,026
  • 9
  • 18
  • Thank you for the answer Garrett, but my settings.py looks exactly like the one you posted! :/ – Gustavo Rangel May 07 '17 at 19:44
  • Hey @GustavoRangel, I've updated my comment because you were doing this in a Python shell rather than from a view which should work. Try this solution out! – Garrett Kadillak May 07 '17 at 19:52
  • Trying the first example with django 2.2, and getting the error, `ImproperlyConfigured: No DjangoTemplates backend is configured.` ... – monkut May 21 '19 at 14:05
  • 1
    This will not work after Django 1.9. I have tried to provide an edit that fixes compatibility with newer versions, but the moderators seem to reject that. I can't post the full code in the comment section, nor does it allow me to format the code correctly. But the setup part should now look like this: `import django` `from django.conf import settings` `from django.template import Template, Context` `TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates'}]` `settings.configure(TEMPLATES=TEMPLATES)` `django.setup()` – Mark Laagland Mar 24 '20 at 10:41
0
    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],  # For admin to work
        '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',
            ],
        },
    },
    {'BACKEND': 'django.template.backends.jinja2.Jinja2',
        'DIRS': [BASE_DIR / 'templates']
     }
   ]

Some dependencies may also need os.path imported or str() placed around the Path.

Gavin
  • 131
  • 5