2017-05-07 10 views
1

を設定されていない私はジャンゴを使用していると私は動的に与えられたテンプレート(lab.html)からHTMLファイルを生成する必要があります。しかし、私はこの奇妙なエラーを取得しておくDjangoのエラー:DjangoTemplatesのバックエンドが

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. 

答えて

3

ジャンゴ> 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

EDIT: コマンドラインではなく、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'}) 
+0

回答ありがとうございます。私のsettings.pyは投稿したものとまったく同じです! :/ –

+0

ちょっと@GustavoRangel、私はあなたがPythonシェルではなく、動作するはずのビューからこれをやっていたので、私のコメントを更新しました。この解決策を試してみてください! –

関連する問題