5

我知道很多人都问过这个问题,但是尽管对我的模板目录的路径进行了硬编码,但我似乎无法让 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 没有被使用(从它的样子来看)

4

4 回答 4

9

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

:(

于 2012-07-11T05:28:16.377 回答
5

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

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",
)
于 2012-07-10T22:36:50.547 回答
1

我打赌/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 中,然后复制所有文件。

于 2012-07-10T21:46:41.233 回答
1

尝试将项目路径添加到 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()
于 2012-07-10T23:14:14.513 回答