Now we might be able to get away with putting our templates directly
in polls/templates (rather than creating another polls subdirectory),
but it would actually be a bad idea. Django will choose the first
template it finds whose name matches, and if you had a template with
the same name in a different application, Django would be unable to
distinguish between them. We need to be able to point Django at the
right one, and the easiest way to ensure this is by namespacing them.
That is, by putting those templates inside another directory named for
the application itself.
首先,Django不需要这个特定的文件夹结构来工作,它只是一个既定的模式。当然,正如official doc所指出的那样,它有一个基本原理:
对于具体情况,可以引用this question或that another。在
简而言之,通过遵循此模式,您可以将模板分为两组:
TEMPLATES['DIRS']
设置所指向的目录中'./appname/templates/appname/'
(并且TEMPLATES['APP_DIRS']
必须为真)。这样可以避免文件夹内文件和外部文件之间的名称冲突。在相关问题 更多 >
编程相关推荐