【问题标题】:Django Error: No DjangoTemplates backend is configuredDjango 错误:没有配置 DjangoTemplates 后端
【发布时间】:2017-10-05 15:56:00
【问题描述】:

我正在使用 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.

【问题讨论】:

    标签: python django


    【解决方案1】:

    对于 Django > 1.7 你的settings.py 你应该在TEMPLATES 列表中对BACKEND 键有一些价值。基本上你的设置中应该有这样的东西

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [],
            'APP_DIRS': True,
            'OPTIONS': {
                # ... some options here ...
            },
        },
    ]
    

    来自文档:

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

    Link

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

    如果您不想使用磁盘中的模板,可以使用以下方法:

    from django.conf import settings
    settings.configure()
    from django.template import Template, Context
    Template('Hello, {{ name }}!').render(Context({'name': 'world'}))
    

    但是,如果您想从磁盘加载模板,则需要付出更多努力。这样的事情会起作用:

    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 的回答,但我的 settings.py 看起来与您发布的完全一样! ://
    • 嘿@GustavoRangel,我更新了我的评论,因为您是在 Python shell 中执行此操作,而不是从应该工作的视图中执行此操作。试试这个解决方案!
    • 尝试使用 django 2.2 的第一个示例,并得到错误,ImproperlyConfigured: No DjangoTemplates backend is configured. ...
    • 这在 Django 1.9 之后将不起作用。我试图提供一个修复与较新版本的兼容性的编辑,但版主似乎拒绝了这一点。我无法在评论部分发布完整的代码,也不允许我正确格式化代码。但是设置部分现在应该是这样的:import djangofrom django.conf import settingsfrom django.template import Template, ContextTEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates'}]settings.configure(TEMPLATES=TEMPLATES)django.setup()
    【解决方案2】:
        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 周围。

    【讨论】:

      猜你喜欢
      • 2012-01-15
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      相关资源
      最近更新 更多