我尝试在我的HTML模板上保持一个有点一致的命名方案.即index.html for main,delete.html for delete page等等.但是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'),
)
Run Code Online (Sandbox Code Playgroud)
我试过改变顺序TEMPLATE_LOADERS,没有成功.
按照Ashok的要求编辑:
每个应用程序的目录结构:
templates/
index.html
add.html
delete.html
create.html
models.py
test.py
admin.py
views.py
Run Code Online (Sandbox Code Playgroud)
在每个应用程序的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())
Run Code Online (Sandbox Code Playgroud) django 模板文件夹需要创建一个以应用程序名称命名的子文件夹,其中包含模板文件。为什么这是必要的,什么时候python manage.py collectstatic可以在遍历所有目录时推断出这些信息?看起来非常多余。