【发布时间】:2017-12-07 12:47:13
【问题描述】:
我试图在我的 django 项目中打开并阅读这个 html 文件,但是我得到了这个 [Errno 2] 没有这样的文件或目录:'test.html'。
html = 'test.html'
open_html = open(html, 'r')
soup = BeautifulSoup(open_html, "html5lib")
open_html.close()
但模板路径在渲染时似乎工作正常
template = 'test.html'
context = {
}
return render(request, template, context)
TEMPLATES = [
'DIRS': [os.path.join(BASE_DIR, "templates")],
]
我知道我的模板应该放在我的应用文件夹中,但是我喜欢在开发和调试时将它们保存在一个文件夹中。
【问题讨论】:
-
html = 'test.html' open_html = open(html, 'r')此代码不采用完整路径。 html 只有文件名 test.html 但 BeautifulSoup 需要完整路径。而不是html = 'test.html'这提供了文件的实际路径。