如果重要的话,我有这样的目录结构(根据satchmo文档,这是默认的推荐结构):
site
- apps
| - __init__.py
- config
- projects
| - site
| - home
| - templates
| - about.html
| - home.html
| - models.py, views.py, admin.py
| - __init__.py
| - local_settings.py
| - settings.py
| - urls.py
| - wsgi.py
| - __init__.py
- static
| - css
| - images (maybe this got autogenerated?)
| - js
| - media
- templates
| base.html
- manage.py
我的URL有about.html和home.html的条目,这两个条目都扩展了base.html。然而,当我访问URL时,我会得到一个通用的satchmo页面,其中包含我从about和home中包含的一些文本,但它根本没有扩展base.html。在我安装satchmo之前,我可以确认这是有效的,但现在我不确定出了什么问题。我假设它正在扩展其他base.html,因为如果我将扩展改为master.html,它会抛出TemplateDoesNotExist异常(我也不确定如何解决)。我的设置中有以下内容。py:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
TEMPLATE_DIRS = (
'templates',
)
如果我将templates目录移到项目中的站点文件夹中,它似乎可以工作,但我不希望它在那里。我试着添加'..//templates"到TEMPLATE_DIRS,但这也不起作用,即使起作用了,我也不确定这将如何与我在应用程序文件夹的某些级别下声明的模板交互。解决这个问题的正确方法是什么?
TEMPLATE_DIRS
条目应该是绝对路径。你可以这样做:
import os
from os.path import normpath, abspath, dirname, join
BASE_DIR = normpath(abspath(join(dirname(__file__), '..', '..')))
TEMPLATE_DIRS = (
join(BASE_DIR, 'templates'),
)
如果你的master.html在你的templates目录中,那么这个错误也应该被修复。
BASE_DIR
的is"基础"是dirname(__file__)
,它返回包含当前文件settings.py
的目录。然后,结果是join
ed和'..'
两次,也就是说,我们向上走两个目录,所以现在我们在顶部的"site"目录中。我们调用abspath
来确保它是一条绝对路径,调用normpath
来删除双斜杠等。