【问题标题】:Django can't find templateDjango 找不到模板
【发布时间】:2012-07-10 11:16:40
【问题描述】:

我知道很多人都问过这个问题,但是尽管对我的模板目录的路径进行了硬编码,但我似乎无法让 Django 找到我的模板。

这里是 settings.py

TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
#django.template.loaders.eggs.Loader',
)

TEMPLATE_DIRS = (
    "/Users/full/path/to/marketing_site/templates",
)

这是views.py文件:

def static (request, feature_page):

# this will get the appropriate feature page template.
template = loader.get_template('features/pricing.html')
c = RequestContext(request,{
})
return HttpResponse(template.render(c))

主文件夹内是模板文件夹和应用程序文件夹。我曾经将应用程序放在与 settings.py 相同的文件夹中,但似乎 django 1.4 更改了默认文件结构。

我的错误是:

TemplateDoesNotExist at /features/pricing 

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/Library/Python/2.7/site-packages/django/contrib/auth/templates/feature_pricing.html (File does not exist)

更新:
我的网页日志将 TEMPLATE_DIRS 列为 ()。

如果我在 TEMPLATE_DIRS 的 settings.py 页面上放置一个打印语句,我会适当地打印出 TEMPLATE_DIRS...所以不知何故 TEMPLATE_DIRS 没有被使用(从它的样子来看)

【问题讨论】:

  • 我假设您使用的是 Windows?
  • 您是否已将项目路径添加到 python sys 路径中?
  • 不,我使用的是 OS X...我没有将项目路径添加到 python sys 路径,我该怎么做?
  • 我应该提到我使用的是 runserver 测试参数而不是 apache 或 nginx 来加载网页

标签: python django django-templates


【解决方案1】:

我在 settings.py 中添加了一个额外的 TEMPLATE_DIR

:(

【讨论】:

  • 啊!刚发现我的问题是一样的。它已经停止工作好几天了......:D
【解决方案2】:

最好为路径变量设置相对路径。你可以这样设置:

import os

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

...

...

TEMPLATE_DIRS = (
    PATH_PROJECT + '/templates/'
)

还假设您使用的是 Windows,您可能想尝试一下:

TEMPLATE_DIRS = (
"C:/Users/full/path/to/marketing_site/templates",
)

【讨论】:

    【解决方案3】:

    我打赌/Users/full/path/to/marketing_site/templates 不包含features 目录,或者/Users/full/path/to/marketing_site/templates/features 不包含pricing.html 文件。

    根据您的评论,您似乎在调用loader.get_template('feature_pricing.‌​html'),而不是使用'feature/pricing.html'

    编辑:我之前没有注意到这一点:

    主文件夹内是模板文件夹和应用程序文件夹。我曾经将应用程序放在与 settings.py 相同的文件夹中,但似乎 django 1.4 更改了默认文件结构。

    这可能是导致问题的原因。更改目录结构以匹配 1.4 Django 约定。您可能只是重新创建项目,将相关设置复制到新创建的 settings.py 中,然后复制所有文件。

    【讨论】:

    • 目录和权限都可以...路由存在...这里有更多错误代码:Django尝试加载这些模板,按以下顺序:使用加载程序django.template.loaders.filesystem。加载器:使用加载器django.template.loaders.app_directories.Loader:/Library/Python/2.7/site-packages/django/contrib/auth/templates/feature_pricing.html(文件不存在)
    【解决方案4】:

    尝试将项目路径添加到 django.wsgi

    import os
    import sys
    
    paths = ('path/to/project/',
            'path/to/more /included/directories/',
        )
    
    for path in paths:
        if path not in sys.path:
           sys.path.append(path)
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
    
    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()
    

    【讨论】:

    • 我现在正在使用 runserver 测试机制...这会有什么不同吗?
    猜你喜欢
    • 2018-01-02
    • 2015-03-14
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    相关资源
    最近更新 更多