【发布时间】:2011-03-06 19:00:19
【问题描述】:
我尝试在我的 HTML 模板上保持某种一致的命名方案。 IE。 index.html 用于主页面,delete.html 用于删除页面等等。但是app_directories 加载器似乎总是从按字母顺序排列的应用程序中加载模板。
有什么方法可以始终首先检查调用应用的templates 目录中的匹配项吗?
相关设置在我的settings.py:
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
TEMPLATE_LOADERS = (
'django.template.loaders.app_directories.load_template_source',
'django.template.loaders.filesystem.load_template_source',
)
TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH, 'templates'),
)
我尝试更改TEMPLATE_LOADERS 的顺序,但没有成功。
按 Ashok 的要求进行编辑:
每个应用的目录结构:
templates/
index.html
add.html
delete.html
create.html
models.py
test.py
admin.py
views.py
在每个应用的views.py中:
def index(request):
# code...
return render_to_response('index.html', locals())
def add(request):
# code...
return render_to_response('add.html', locals())
def delete(request):
# code...
return render_to_response('delete.html', locals())
def update(request):
# code...
return render_to_response('update.html', locals())
【问题讨论】:
-
您能否提供“模板”目录的结构以及如何加载/渲染模板的示例代码
-
app_directories 加载器听起来有点误导我。我会假设它会像您一样从正确的应用程序目录加载模板。毕竟,django 应用程序的意义不在于能够创建分离的切片吗?