0

在搜索一堆主题并尝试各种事情之后,我真的很难正确设置 TEMPLATE_DIR 。

这是我的项目设置:

#settings.py
DEBUG = True
TEMPLATE_DEBUG = DEBUG
import os
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

MEDIA_ROOT = ''
MEDIA_URL = ''

STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    PROJECT_PATH + '/static/',
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)
TEMPLATE_DIRS = (
    PROJECT_PATH + '/TrainingBook/templates/',
    PROJECT_PATH + '/RestClient/templates/',
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'TrainingBook',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    # 'django.core.context_processors.request',
    "TrainingBook.context_processors.global_context",
)
print PROJECT_PATH # /Users/Kuba/Development/University/RestClient
print STATICFILES_DIRS # ('/Users/Kuba/Development/University/RestClient/static/',)
print TEMPLATE_DIRS 
# ('/Users/Kuba/Development/University/RestClient/TrainingBook/templates/',
# '/Users/Kuba/Development/University/RestClient/RestClient/templates/')

我的项目结构:

$ pwd
/Users/Kuba/Development/University/RestClient
$ tree
.
├── RestClient
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.pyc
│   ├── templates
│   │   ├── base.html
│   │   ├── home.html
│   │   └── login_form.html
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── TrainingBook
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── context_processors.py
│   ├── context_processors.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── templates
│   │   ├── friends.html
│   │   ├── statistics.html
│   │   └── workouts.html
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
├── manage.py
├── settings.py
├── settings.pyc
└── static
    ├── css
    │   ├── bootstrap-glyphicons.css
    │   ├── bootstrap.css
    │   ├── bootstrap.min.css
    │   └── main.css
    └── js
        ├── bootstrap.js
        ├── bootstrap.min.js
        └── jquery-1.10.2.js

我将“settings.py”上移了一级,以便将 PROJECT_PATH 设置为“/RestClient/”而不是“/RestClient/RestClient/”。

我还修改了 manage.py 从

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "RestClient.settings")

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

当我运行服务器 TemplateDoesNotExist 时,我看到了一些奇怪的东西:

Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/Users/Kuba/Development/University/RestClient/TrainingBook/templates/templates/home.html (File does not exist)
/Users/Kuba/Development/University/RestClient/RestClient/templates/templates/home.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/Users/Kuba/.virtualenvs/client/lib/python2.7/site-packages/django/contrib/auth/templates/templates/home.html (File does not exist)
/Users/Kuba/Development/University/RestClient/TrainingBook/templates/templates/home.html (File does not exist)

如您所见,即使我没有指定它,也有“/templates/templates”。

另一方面,如果我将 TEMPLATE_DIRS 切换为:

TEMPLATE_DIRS = (
    PROJECT_PATH + '/TrainingBook/',
    PROJECT_PATH + '/RestClient/',
)

在引发 TemplateDoesNotExist 之后,我可以看到加载程序正在寻找模板:

/Users/Kuba/Development/University/RestClient/RestClient/home.html
/Users/Kuba/Development/University/RestClient/TrainingBook/home.html

我做错了什么?

编辑:问题是我定义了一些这样的视图:

class Home(Base):
    template_name = 'templates/home.html'
4

1 回答 1

1

您指定templates/home.html而不是只是home.html.

模板名称将附加到 TEMPLATE_DIRS,因此如果是模板名称,则/foo/templates/TEMPLATE_DIRS 将变为。相反,模板名称应该是正确的,并且生成的模板路径应该是正确的。/foo/templates/templates/home.htmltemplates/home.htmlhome.html/foo/templates/home.html

于 2013-08-18T04:36:30.567 回答