[python][Django 1.8]TemplateDoesNotExist异常

博主在win7系统下使用django中遇到了该问题,查了下stackoverflow,发现一般的解决办法如下:

在setting.py中的添加

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

 

 

博主添加后依然出现此问题,最后在错误页面的Traceback 中找到了问题

Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
F:\python2.7\lib\site-packages\django-1.8.2-py2.7.egg\django\contrib\admin\templates\maby.html (File does not exist)
F:\python2.7\lib\site-packages\django-1.8.2-py2.7.egg\django\contrib\auth\templates\maby.html (File does not exist)

Django并没有去博主指定的文件夹中寻找模板文件,而是去到了两个默认的文件夹中寻找,随再次寻找答案,最终找到如下办法:

 

修改setting.py,添加以下内容:

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates/').replace('\\', '/')],
        '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',
            ],
        },
    },
]

再次启动后,页面成功加载。

希望能帮助到你,国内几乎没有什么资料=。=

posted @ 2015-07-07 17:17  yuexizhuo  阅读(2690)  评论(0编辑  收藏  举报