全面解析Django的模板路径设置templates(settings.py)

一、首先明白几个重要的概念:
1、os.path.dirname(__file__)  #指的是 当前文件所在目录,即settings.py在哪个文件夹里。
用print os.path.dirname(__file__)  #输出结果 D:\MyPython\LearnDJ\src\LearnDJ\book

2、os.path.abspath(__file__) #指的是 当前文件的绝对路径,包括文件名
用print os.path.abspath(__file__) #输出结果 
D:\MyPython\LearnDJ\src\LearnDJ\book\settings.py

3、os.path.dirname(os.path.dirname(__file__)) #指的是 当前文件所在目录的所在目录,即上一层文件夹。
用print os.path.dirname(os.path.dirname(__file__)) #输出结果 D:\MyPython\LearnDJ\src\LearnDJ
由此可知“os.path.dirname()”具有将目录设为上一层概念!

二、其次我们再讨论如何设置TEMPLATE_DIRS

import os
BASE_DIR =os.path.dirname(os.path.dirname(__file__))

TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, 'templates'),
)

这样设置之后,在工程文件夹内,建立templates文件夹(和manage.py同级)#D:\MyPython\LearnDJ\src\LearnDJ\templates。然后你可以将你的模板文件.html放于其中

猜你喜欢

转载自blog.csdn.net/uwenhao2008/article/details/80622316