python render_to_response 给出 TemplateDoesNotExist

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1957089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:26:21  来源:igfitidea点击:

render_to_response gives TemplateDoesNotExist

pythondjangodjango-templates

提问by dhaval

I am obtaining the path of template using

我正在使用获取模板的路径

paymenthtml = os.path.join(os.path.dirname(__file__), 'template\payment.html')

and calling it in another application where paymenthtml gets copied to payment_template

并在另一个应用程序中调用它,其中 paymenthtml 被复制到 payment_template

return render_to_response(self.payment_template, self.context, RequestContext(self.request))

But I get error

但我得到错误

TemplateDoesNotExist at /test-payment-url/

E:\testapp\template\payment.html

TemplateDoesNotExist 位于 /test-payment-url/

E:\testapp\template\payment.html

Why is the error coming?

为什么会出现错误?

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?

编辑:我在 settings.py 中进行了以下更改并且它能够找到模板,但是我无法在生产中对路径进行硬编码,有什么线索吗?

TEMPLATE_DIRS = ("E:/testapp" )

回答by John Debs

It seems like Django will only load templates if they're in a directory you define in TEMPLATE_DIRS, even if they exist elsewhere.

似乎 Django 只会加载位于您定义的目录中的模板TEMPLATE_DIRS,即使它们存在于其他地方。

Try this in settings.py:

在 settings.py 中试试这个:

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
# Other settings...
TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, "templates"),
)

and then in the view:

然后在视图中:

return render_to_response("payment.html", self.context, RequestContext(self.request))
# or
return render_to_response("subdir/payment.html", self.context, RequestContext(self.request))

This would render either E:\path\to\project\templates\payment.htmlor E:\path\to\project\templates\subdir\payment.html. The point is that they're inside of the directory we specified in settings.py.

这将呈现E:\path\to\project\templates\payment.htmlE:\path\to\project\templates\subdir\payment.html。关键是它们位于我们在 settings.py 中指定的目录中。

回答by pcv

By the way: A tricky thing is, that django throws TemplateDoesNotExisteven if the rendered template includes a template that doesn't exist - {% include "some/template.html" %}... this knowledge has cost me some time and nerves.

顺便说一句:一个棘手的事情是,TemplateDoesNotExist即使渲染的模板包含一个不存在的模板,django也会抛出- {% include "some/template.html" %}......这些知识让我花费了一些时间和精力。

回答by joetsuihk

i do not have a django here, but i think you should use / instead of \\ ?

我这里没有 django,但我认为你应该使用 / 而不是 \\ ?

python helps you about the slashes across OSes

python 帮助你解决跨操作系统的斜线问题

回答by Andrew Hare

Are you certainthat this file exists on your system?

确定该文件存在于您的系统中吗?

E:\testapp\template\payment.html

E:\testapp\template\payment.html

This error message is pretty straightforward and is seen when Django tries to find your template file by path on the file system and cannot see it.

此错误消息非常简单,当 Django 尝试通过文件系统上的路径查找模板文件但无法看到它时,就会看到此错误消息。

If the file does exist the next step would be to check the permissions on that file and the directories to ensure that this is not a permissions issue. If your E:drive is a mapped network drive of some network share then you also need to check sharing permissions as well.

如果该文件确实存在,下一步将是检查该文件和目录的权限,以确保这不是权限问题。如果您的E:驱动器是某个网络共享的映射网络驱动器,那么您还需要检查共享权限。