Django模板dir奇怪的行为

时间:2013-08-17 20:10:14

标签: python django

在搜索一堆主题并尝试各种各样的事情后,我确实遇到了设置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'

1 个答案:

答案 0 :(得分:1)

您指定了templates/home.html而非home.html

模板名称将附加到TEMPLATE_DIRS,因此如果/foo/templates/是模板名称,则/foo/templates/templates/home.html作为TEMPLATE_DIRS将变为templates/home.html。相反,模板名称应为home.html,结果模板路径为/foo/templates/home.html,这是正确的。