【问题标题】:TemplateDoesNotExist - file exists, no permissions issueTemplateDoesNotExist - 文件存在,没有权限问题
【发布时间】:2011-06-25 22:33:22
【问题描述】:

尝试在 django 中呈现模板时收到此错误:

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:

这是我的 settings.py 条目:

SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
    os.path.join(SETTINGS_PATH, 'template'),
)

这是我的观点:

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

    return render_to_response(request, "template/apps.html",{"new_app_form":NewAppFormSet})

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

【问题讨论】:

  • 碰巧apps.html 中是否有一个include 模板标签引用了一个不存在的模板?
  • 嗯,我正在扩展另一个模板 - 我继续删除了它(以及用一些基本标记替换了整个模板),仍然没有运气。不过很好,谢谢。

标签: django django-templates


【解决方案1】:

我遇到了一个非常相似的问题,这让我发疯了。原来我用来学习Python/Django的那本书并没有说要在settings.py中明确设置templates目录。很混乱。我正在使用 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'),
)

感谢stackoverflow!!

【讨论】:

    【解决方案2】:

    我不确定我遵循的哪个示例指定了 render_to_response 快捷方法需要传递视图的请求对象,但它就像将其更改为一样简单:

    return render_to_response("template/apps.html",{"new_app_form":NewAppFormSet()})
    

    【讨论】:

    【解决方案3】:

    尝试使用 os.path.realpath 分配 SETTINGS_PATH:

    SETTINGS_PATH = os.path.realpath(os.path.dirname(__file__))
    

    【讨论】:

    • 我不确定我遵循的哪个示例指定了 render_to_response 快捷方法需要传递视图的请求对象,但它就像将其更改为一样简单:return render_to_response("template/apps.html",{"new_app_form":NewAppFormSet})
    【解决方案4】:

    您可能已经复制粘贴了您现在拥有的TEMPLATE_DIRS,您是否将默认设置保留在SETTINGS 文件的下方?

    【讨论】:

      【解决方案5】:

      来自我的 settings.py:

      BASE_DIR = os.path.dirname(__file__)
      
      def RELATIVE_PATH(*args):
          return os.path.join(BASE_DIR, *args)
      
      TEMPLATE_DIRS = (
          RELATIVE_PATH('template'),
      )
      

      【讨论】:

        【解决方案6】:

        如果你尝试怎么办

        return render_to_response(request, "apps.html",{"new_app_form":NewAppFormSet})
        

        而不是

        return render_to_response(request, "template/apps.html",{"new_app_form":NewAppFormSet})
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-10-13
          • 1970-01-01
          • 2016-04-29
          • 2011-09-17
          • 2018-10-30
          • 2017-04-12
          相关资源
          最近更新 更多