5

I'm building an application, that uses the Django templating engine / language to "compile" some HTML. However, the app doesn't run on Django, and doesn't have all the config and stuff. When I try to use it, I get the following error:

Traceback (most recent call last):
  File "Send.py", line 33, in <module>
    template = loader.get_template("email.html")
  File "/Library/Python/2.7/site-packages/django/template/loader.py", line 157, in get_template
    template, origin = find_template(template_name)
  File "/Library/Python/2.7/site-packages/django/template/loader.py", line 138, in find_template
    raise TemplateDoesNotExist(name)
django.template.base.TemplateDoesNotExist: email.html

The code I am using is the following:

from django.template import loader, Context
from django.conf import settings

template = loader.get_template("email.html")
rendered = template.render(data)

The template is in the same directory as the Python file.

  • 2
    Possible [duplicate](http://stackoverflow.com/questions/98135/how-do-i-use-django-templates-without-the-rest-of-django) – reclosedev Jan 25 '12 at 12:45
  • Here's an answer that works on Django >= 1.8 http://stackoverflow.com/a/34494931/817277 – Pramod Dec 28 '15 at 14:03

3 Answers3

8

I'd recommend using Jinja2 instead of Django templates as a standalone solution.

Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83
  • 1
    I second that. If you're already familiar with Django templates you'll know how to use Jinja2. They are almost identical but Jinja is better imho. – GivP Jan 25 '12 at 12:49
  • Jinja2 python3 support is still experimental – niklas Jul 26 '17 at 12:49
6

Have you tried to use this?

settings.configure(TEMPLATE_DIRS=('.',))

Since the templates are in the same directory as the python code, this should be enough for the loader to find the template.

jcollado
  • 39,419
  • 8
  • 102
  • 133
3

In order for the loader to find your templates; you need to have the directory where the templates are located in your TEMPLATE_DIRS tuple in settings.py. By default django will search for a templates directory inside an application so if you don't have an application registered; django cannot find templates.

If you are after a "django like" template engine, jinja is very close to django's syntax and is completely self-contained, you won't run into unexpected issues as you are facing now.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284