Django18初体验


1181 浏览 6 years, 1 month

7.8 模板配置

版权声明: 转载请注明出处 http://www.codingsoho.com/

创建主页视图函数如下

# Create your views here.
def home(request):
    context = {}
    return render(request, "home.html", context)

如果不创建template的话, http://127.0.0.1:8000/ 将会报告下面的错误调试信息

TemplateDoesNotExist 
Template-loader postmortem
Django tried loading these templates, in this order:
•   Using loader django.template.loaders.filesystem.Loader: 
•   Using loader django.template.loaders.app_directories.Loader: 
    o   D:\virtualdir\trydjango18\lib\site-packages\django\contrib\auth\templates\home.html (File does not exist)
    o   D:\virtualdir\trydjango18\lib\site-packages\django\contrib\admin\templates\home.html (File does not exist)
    o   D:\virtualdir\trydjango18\lib\site-packages\django\contrib\admindocs\templates\home.html (File does not exist)

在newsletter下面创建template目录,并且在“INSTALLED_APPS”下面添加“newsletter”,django将会搜索该template目录

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'newsletter'
)

如果将newsletter注释掉,django会不找不到模板文件

Template-loader postmortem
Django tried loading these templates, in this order:
•   Using loader django.template.loaders.filesystem.Loader: 
•   Using loader django.template.loaders.app_directories.Loader: 
    o   D:\virtualdir\trydjango18\lib\site-packages\django\contrib\auth\templates\home.html (File does not exist)
    o   D:\virtualdir\trydjango18\lib\site-packages\django\contrib\admin\templates\home.html (File does not exist)
    o   D:\virtualdir\trydjango18\lib\site-packages\django\contrib\admindocs\templates\home.html (File does not exist)
    o   D:\virtualdir\trydjango18\src\newsletter\templates\home.html (File does not exist)

相对于前面的报错,这个最后一行是新加的D:\virtualdir\trydjango18\src\newsletter\templates\home.html (File does not exist),它是搜索对应的应用的模板路径

在“newsletter”下面创建文件“home.html”, it works

本例子中,我们把templates从application目录中移到root文件夹,在src目录创建templates文件夹

修改settings.py, 更新TEMPLATES里的DIRS

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "templates")],
        'APP_DIRS': True, # Whether the engine should look for template source files inside installed applications.
        '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',
            ],
        },
    },
]

如果home.html没有创建,django会抛出下面异常,否则, it works.

Template-loader postmortem
Django tried loading these templates, in this order:
•   Using loader django.template.loaders.filesystem.Loader: 
    o   D:\virtualdir\trydjango18\src\templates\home.html (File does not exist)
•   Using loader django.template.loaders.app_directories.Loader: 
    o   D:\virtualdir\trydjango18\lib\site-packages\django\contrib\auth\templates\home.html (File does not exist)
    o   D:\virtualdir\trydjango18\lib\site-packages\django\contrib\admin\templates\home.html (File does not exist)
    o   D:\virtualdir\trydjango18\lib\site-packages\django\contrib\admindocs\templates\home.html (File does not exist)

这一行D:\virtualdir\trydjango18\src\templates\home.html (File does not exist)就是搜索的新指定的模板路径下搜索文件