Django错误:未配置DjangoTemplates后端 [英] Django Error: No DjangoTemplates backend is configured

查看:155
本文介绍了Django错误:未配置DjangoTemplates后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django,我需要根据给定的模板(lab.html)动态生成HTML文件:

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.


推荐答案

对于Django> 1.7您的 settings.py ,您应该在 TEMPLATES 列表中的 BACKEND 键中具有一些值。基本上,您应该在设置中添加类似的内容

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 ...
        },
    },
]

从文档开始:

BACKEND 是实现Django模板后端API的模板引擎类的虚线Python路径。内置的后端是 django.template.backends.django.DjangoTemplates django.template.backends.jinja2.Jinja2

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.

链接

编辑:
要从命令行而不是使用 render 的视图加载模板方法,您必须做更多的工作。

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'})

这篇关于Django错误:未配置DjangoTemplates后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆