Django错误:没有配置DjangoTemplates后端

时间:2017-05-07 17:00:45

标签: python django

我正在使用Django,我需要从给定模板(lab.html)动态生成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

然而,我继续得到这个奇怪的错误,经过一些研究我没有找到任何地方。有什么想法吗?

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.

1 个答案:

答案 0 :(得分: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']
     }
   ]

某些依赖项可能还需要导入 os.path 或将 str() 放在 Path 周围。