【发布时间】:2014-05-12 07:35:58
【问题描述】:
import os.path
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
#os.path.join(BASE_DIR,'templates'),
)
我想知道 TEMPLATE_DIRS 到底是什么,所以我在 Django shell 上运行它,
>>> import os.path
>>> os.path.dirname(__file__)
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name '__file__' is not defined
为什么这种合法属性在shell中失败了?我确实在 shell 中做了一些测试,并提出了另一个问题:
>>> os.path.dirname(os.path.dirname('/user/name'))
'/'
>>> os.path.dirname('/user/name')
'/user'
>>> B = os.path.dirname(os.path.dirname('/user/name'))
>>> os.path.join(B,'template')
'/template'
带注释的代码os.path.join(BASE_DIR,'templates'),不就是'/templates'出来的吗?
不是根目录吗?如果是这样,Django 如何在我没有的目录中搜索我的模板文件?
【问题讨论】: