使用Django的模板引擎,而不使用Django的其余部分

5 python django templates

我正在构建一个应用程序,它使用Django模板引擎/语言来"编译"一些HTML.但是,该应用程序不能在Django上运行,并且没有所有的配置和东西.当我尝试使用它时,我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

我使用的代码如下:

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

template = loader.get_template("email.html")
rendered = template.render(data)
Run Code Online (Sandbox Code Playgroud)

该模板与Python文件位于同一目录中.

Zau*_*bov 8

我建议使用Jinja2而不是Django模板作为独立的解决方案.


jco*_*ado 6

你试过用这个吗?

settings.configure(TEMPLATE_DIRS=('.',))
Run Code Online (Sandbox Code Playgroud)

由于模板与python代码位于同一目录中,因此加载程序应该足以找到模板.