render_to_response给出TemplateDoesNotExist [英] render_to_response gives TemplateDoesNotExist
问题描述
我正在使用
paymenthtml = os.path.join(os.path.dirname(__file__), 'template\\payment.html')
并在另一个应用程序中调用
paymenthtml被复制到payment_template
and calling it in another application where paymenthtml gets copied to payment_template
return render_to_response(self.payment_template, self.context, RequestContext(self.request))
但是我收到错误
TemplateDoesNotExist at / test-payment-url /
TemplateDoesNotExist at /test-payment-url/
E:\testapp\template\payment.html
E:\testapp\template\payment.html
为什么会出现错误?
编辑:我在settings.py中进行了以下更改,可以找到模板,但是我无法对生产中的路径进行硬编码,任何线索?
Edit : I have made following change in settings.py and it is able to find the template, but i cannot hardcode the path in production, any clue?
TEMPLATE_DIRS = ("E:/testapp" )
推荐答案
似乎Django只会加载模板,如果它们位于您在 TEMPLATE_DIRS
中定义的目录中,即使它们存在于其他位置。
It seems like Django will only load templates if they're in a directory you define in TEMPLATE_DIRS
, even if they exist elsewhere.
尝试此在settings.py:
Try this in settings.py:
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
# Other settings...
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, "templates"),
)
然后在视图中:
return render_to_response("payment.html", self.context, RequestContext(self.request))
# or
return render_to_response("subdir/payment.html", self.context, RequestContext(self.request))
这将呈现 E:\path\to\project\templates\\ \\ payment.html
或 E:\path\to\project\templates\subdir\payment.html
。关键是它们在我们在settings.py中指定的目录中。
This would render either E:\path\to\project\templates\payment.html
or E:\path\to\project\templates\subdir\payment.html
. The point is that they're inside of the directory we specified in settings.py.
这篇关于render_to_response给出TemplateDoesNotExist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!