TemplateDoesNotExist - 文件存在,没有权限问题

Kyl*_*yle 2 django django-templates

尝试在 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:
Run Code Online (Sandbox Code Playgroud)

这是我的 settings.py 条目:

SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
    os.path.join(SETTINGS_PATH, 'template'),
)
Run Code Online (Sandbox Code Playgroud)

这是我的观点,被称为:

def handler(request):
    if request.method == 'GET': 
        NewAppFormSet = modelformset_factory(Application)

    return render_to_response(request, "template/apps.html",{"new_app_form":NewAppFormSet})
Run Code Online (Sandbox Code Playgroud)

我已经尝试了所有可能的路径组合、调整权限等。真正的问题似乎出在模板加载器上,因为它们无法识别 TEMPLATE_DIRS 中列出的任何路径。我对这个字符串进行了硬编码,但无济于事。我也python manage.py runserver以 sudo / 作为 root 运行。我不知所措...

小智 5

我有一个非常相似的问题,这让我发疯。事实证明,我用来学习 Python/Django 的书并没有说我必须在 settings.py 中明确设置模板目录。非常混淆。我正在使用 Python 2.6 和 Django 1.3.1 - 也许以前的版本会自动找到模板目录。无论如何,在把我的头发拉了一天并在这个网站上阅读了很多之后,我发现我必须用 settings.py 中的以下内容替换默认的 TEMPLATE_DIRS 分配:

# Find templates in the same folder as settings.py.
SETTINGS_PATH = os.path.realpath(os.path.dirname(__file__))

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(SETTINGS_PATH, 'templates'),
)
Run Code Online (Sandbox Code Playgroud)

感谢stackoverflow!!