1
我使用Django和我需要動態地從給定模板的HTML文件(lab.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.
謝謝你的答案加勒特,但我的settings.py看起來就像您發佈的一個! :/ –
嘿@GustavoRangel,我已經更新了我的評論,因爲你是在Python shell中而不是從應該工作的視圖中執行此操作。試試這個解決方案! –