【问题标题】:Django can't find template directoryDjango 找不到模板目录
【发布时间】:2015-06-25 20:03:01
【问题描述】:

我知道有很多类似的问题,但还没有解决我的问题。

Python 版本:3.4 Django 版本:1.8

加载 http://127.0.0.1:8000/lfstd/ 时,我在 /lfstd/ 处得到 TemplateDoesNotExist

system.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
TEMPLATE_DIRS = (
    TEMPLATE_PATH,
)

lfstd/views.py:

def index(request):
[...]
return render(request, 'lfstd/index.html', context_dict)

项目 urls.py:

from django.conf.urls import include, url
from django.contrib import admin

url(r'^admin/', include(admin.site.urls)),
url(r'^lfstd/', include('lfstd.urls')),

lfstd urls.py:

from django.conf.urls import patterns, url
from lfstd import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'))

在 base_dir 和 template_path 上运行打印,我得到:

Base dir: C:\Users\Phil\PycharmProjects\TownBuddies
Template path: C:\Users\Phil\PycharmProjects\TownBuddies\templates

这正是我的项目和模板文件夹所在的位置。但是,django 不会在该文件夹中查找模板。它在以下文件夹中查找:

C:\Python34\lib\site-packages\django\contrib\admin\templates\lfstd\index.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\lfstd\index.html (File does not exist)

事实上,如果我将我的模板移到那里,它确实会找到它并且可以正常工作。

非常感谢任何帮助,我刚开始使用 Django 并完全陷入困境......

编辑:

我将 TEMPLATE_DIRS 更改为 TEMPLATES 但 Django 仍然没有查看模板文件夹:

【问题讨论】:

    标签: python django templates directory


    【解决方案1】:

    TEMPLATE_DIRS 设置在 django 1.8 中已弃用。您应该改用TEMPLATES

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [TEMPLATE_PATH],
            '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',
                ],
            },
        },
    ]
    

    【讨论】:

    • 感谢加载,这是一个开始。不过还是不行,请看我的编辑。
    • Postmortem 说 django 没有获取模板目录设置。它仅使用应用程序目录。可能你设置了一些其他键而不是DIRS?我用示例设置更新了答案。
    • 你完全正确。我在你上次发表评论之前发现了它。阅读您的答案后,我只是将 TEMPLATE_DIRS 重命名为 TEMPLATES,并且忽略了已经存在的 TEMPLATES 变量。谢谢!
    【解决方案2】:

    我正在使用 pytest-django 进行一些单元测试,但突然我得到重复且完全出乎意料的 ** TemplateDoesNotExist** 错误。

    我在试图弄清楚发生了什么时发现了这个 stackoverflow 问题。

    我终于通过删除所有__pycache__ 目录解决了这个问题。从那以后,我了解到,当事情突然变得莫名其妙时,要清除 __pycache__,然后再尝试修复它们来破坏它们!

    我使用这个脚本递归地清除了所有 __pycache__ 目录,其中包含以下内容的模块:

    #!/usr/bin/env python
    import os
    import shutil
    
    #!/usr/bin/env python
    
    import os
    import shutil
    
    BASE_DIR = os.path.abspath(__file__)
    print(BASE_DIR)
    
    for root, dirs, files in os.walk(BASE_DIR):
        for directory in dirs:
            if directory == '__pycache__':
                shutil.rmtree(os.path.join(root, directory))
    

    【讨论】:

      猜你喜欢
      • 2012-07-18
      • 2012-03-20
      • 2018-01-02
      • 2012-07-10
      • 2015-03-14
      • 2020-05-27
      • 1970-01-01
      相关资源
      最近更新 更多