【问题标题】:Django can't find location to load templates/css fromDjango 找不到从中加载模板/css 的位置
【发布时间】:2015-06-09 17:31:37
【问题描述】:

我通过设置在 settings.py 文件中包含了我的模板文件夹的路径:

TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')

STATIC_FILES_DIR 也是如此,但我不断收到 TemplateDoesNotExist 错误。当我查看错误的事后分析时,我看到:

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

为什么 template.loader 会在该目录中查找模板文件,而不是在我的 settings.py 文件中指定的文件?此外,当我将home.html 页面复制到错误消息中提到的第一个目录中时,页面加载内容时不会出错,那么如何让加载程序从它正在搜索的位置移动到文件所在的目录实际位于?

【问题讨论】:

    标签: python django templates django-templates django-settings


    【解决方案1】:

    有效的设置名称是TEMPLATE_DIRS,它是一个字符串元组,而不是简单的字符串:

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

    【讨论】:

    • 给未来访问者的注意事项:从 Django 1.8 开始,此设置已弃用,取而代之的是 TEMPLATES
    • 还有其他可能的解释吗?即使我进行此更改,错误仍然存​​在。
    • @Selcuk 是的,你说得对。但是设置仍然是复数 DIRS 和元组类型:-)
    • 根据错误 django 仅使用应用程序模板目录。如果您使用 django 1.8,请阅读 @Selcuk 关于弃用此设置的评论并将其更改为 TEMPLATES
    • 这是在 Django 1.7 中
    猜你喜欢
    • 2013-10-12
    • 2015-11-13
    • 1970-01-01
    • 2014-02-28
    • 2020-06-14
    • 2012-10-01
    • 2012-03-09
    • 2016-02-26
    • 2017-04-20
    相关资源
    最近更新 更多