在Djang中获取模板doesnoteexsist错误

2024-06-02 08:43:00 发布

您现在位置:Python中文网/ 问答频道 /正文

TemplateDoesNotExist at /
index.html
Request Method: GET
Request URL:    
Django Version: 1.6.5
Exception Type: TemplateDoesNotExist
Exception Value:    
index.html
Exception Location: C:\Python27\lib\site-packages\django\template\loader.py in find_template, line 131

Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
C:\Users\Peter Na\documents\github\tutorial2\static\templates\index.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
C:\Python27\lib\site-packages\django\contrib\admin\templates\index.html (File does not exist)
C:\Python27\lib\site-packages\django\contrib\auth\templates\index.html (File does not exist)

TemplateDoesNotExist at /
index.html
Request Method: GET
Request URL:
Django Version: 1.6.5
Exception Type: TemplateDoesNotExist
Exception Value:    
index.html
Exception Location: C:\Python27\lib\site-packages\django\template\loader.py in find_template, line 131
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.6

Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
C:\Users\Peter Na\documents\github\tutorial2\static\templates\index.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
C:\Python27\lib\site-packages\django\contrib\admin\templates\index.html (File does not exist)
C:\Python27\lib\site-packages\django\contrib\auth\templates\index.html (File does not exist)

在根文件夹中,我有一个静态文件夹>;Templates>;索引.html. 好像它不能提取HTML文件???在


Tags: djangoindexlibpackageshtmlexceptionsitenot
2条回答

您需要在设置文件中添加模板目录。在

import os

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

...

MEDIA_ROOT = PROJECT_PATH + '/media/'

TEMPLATE_DIRS = (
    PROJECT_PATH + '/templates/'
)

我这样做我的项目。我使用的是Djnago 1.6

import os

#Base root is for outside of the project structure
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

#Package root is for inside the project
PACKAGE_ROOT = os.path.abspath(os.path.dirname(__file__))

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(PACKAGE_ROOT, "static"),
]

MEDIA_ROOT = os.path.join(PACKAGE_ROOT, "site_media", "media")

MEDIA_URL = '/media/'

TEMPLATE_LOADERS = [
    "django.template.loaders.filesystem.Loader",
    "django.template.loaders.app_directories.Loader",
]

TEMPLATE_DIRS = [
    os.path.join(PACKAGE_ROOT, "templates"),
]

相关问题 更多 >