Py学习  »  问与答

关于django模板的问题

lowkeynic4 • 10 年前 • 3703 次点击  

我的django版本是1.8.3,我在manager.py同目录下创建了templates文件夹,然后在setting中设置

BASE_DIR = os.path.dirname(os.path.dirname(__file__))    
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),)

然后在views.py中调用templates中的模板base.html,

def test(request) :
    return render(request,'base.html')

url中也设置好了,访问时会显示TemplateDoesNotExist,错误信息中load的路径是如下几个

/usr/local/lib/python2.7/dist-packages/bootstrap_admin/templates/base.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/base.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/templates/base.html (File does not exist)

这其中也没有我的template路径啊,然后我将templates文件夹放到我的app文件夹中,并把setting.py中的TEMPLATE_DIRS设置成

TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'wenzhang/templates'),)

,然后view.py中什么都没改就可以访问了,我如果在view.py中访问个没有的base1.html,在debug信息中显示的是:

/usr/local/lib/python2.7/dist-packages/bootstrap_admin/templates/base1.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/base1.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/templates/base1.html (File does not exist)
/home/tenshine/Documents/django/myblog/wenzhang/templates/base1.html (File does not exist)

最后一条就是我设置的路径了,为什么放在manager.py同目录不行,别人的工程都是放在manager.py目录下啊? 请高手看下这是为什么?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1216
文章 [ 4 ]  |  最新文章 10 年前
cdxfish
Reply   •   1 楼
cdxfish    10 年前

话说 django1.8 的多模板,还真的有点蛋疼

MCC
Reply   •   2 楼
MCC    10 年前

https://docs.djangoproject.com/en/1.8/ref/settings/#std:setting-TEMPLATES

shen_gan
Reply   •   3 楼
shen_gan    10 年前

@lowkeynic4 不建议你删除这个。删除了你现在没有问题,说不定以后的时候就会出问题了。因为下面还有一些配置你都删了,正确的改法为:

TEMPLATES = [
    {
        #...
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        #...
    }
]
lowkeynic4
Reply   •   4 楼
lowkeynic4    10 年前

找到原因了,setting.py中有一个TEMPLATES选项如下

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

将这些都删除都可以了,但是不明白这些东西中的意思,为什么有这些东西,我设置的TEMPLATE_DIRS就不正确工作呢