首页 > 解决方案 > django 模板对大写或小写文件夹名称敏感

问题描述

Ubuntu 18上,使用 Django 2.1.2,我从这个github克隆了一个 repo :在 learning_logs/templates/ 中有一件非常奇怪的事情,我必须为 index.html 创建“Learning_log”,为其他 html 创建“learning_logs” ,如果我只使用一个来放置所有文件,它就会出现 TemplateDoesNotExist。详情如下:

只使用“learning_logs”:

“TemplateDoesNotExist”错误(TemplateDoesNotExist at / Learning_logs/index.html)

只使用“Learnning_logs”:

“TemplateDoesNotExist”错误(TemplateDoesNotExist at /learning_logs/base.html)

好像同时需要“ learning_logs ”“Learning_logs”,

我该如何解决?

谢谢!!

标签: pythondjango

解决方案


这是因为在设置中没有提到调用模板时在哪里查看。

将此添加到模板标签下的设置中,或根据您自己的目的进行修改:

    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'),'./learning_logs/templates','./Learning_logs/templates'],
        'APP_DIRS': True,
        ...
    },
    ]

注意:如果模板文件夹与设置位于同一目录中,则添加os.path.join(BASE_DIR, 'templates'),]'就足够了,否则应添加文件夹的位置,例如,,'./Learning_logs/templates'

另外,看看这个

在您看来,尝试删除learning_logs/这个相对而言:像这样:

return render(request, 'index.html', context)

推荐阅读