简体   繁体   English

Django:如何考虑模板标签包括的结构模板文件夹?

[英]Django : How to structure template folder considering template tag includes?

I have my template folder with all html templates lieing together in the template folder with no directory structure as such. 我的模板文件夹中所有html templates放在模板文件夹中,没有目录结构。

I decided to arrange them on per app basis, but: 我决定按每个应用程序排列它们,但是:

  • A template with template-tags belong to different apps. 具有模板标签的模板属于不同的应用程序。

Eg: Login page(Login app) includes a banner that belongs to UserActivity [User activity app]. 例如:登录页面(登录应用程序)包含一个属于UserActivity [用户活动应用程序]的横幅。 So, if I include the login template in login folder in templates, then it will be including stuff across other app's template folder. 因此,如果我在模板的登录文件夹中包含登录模板,则它将包含其他应用程序的模板文件夹中的内容。

How should I structure so that all that referred stays in 1 place organized ? 我应该如何构造以使所有引用的对象都组织在一个地方?

Feel free to ask for more info.. :) 随时要求更多信息.. :)

Organizing your templates in subdirectories is definitely they way to go, but I am not sure if you can really reach the level of separation you are looking for. 在子目录中组织模板绝对是他们的路要走,但是我不确定您是否真的可以达到所需的分离级别。 If your apps depend on each other you'll always have includes and tags from other apps. 如果您的应用程序相互依赖,您将始终拥有其他应用程序的包含和标签。 So i'd put the templates to the app they belong to. 因此,我会将模板放到它们所属的应用程序中。

But maybe the docs about template loaders can help you clarify your structure. 但是也许有关模板加载器的文档可以帮助您阐明结构。

For example the app_directories.Loader 例如app_directories.Loader

Loads templates from Django apps on the filesystem. 从文件系统上的Django应用加载模板。 For each app in INSTALLED_APPS, the loader looks for a templates subdirectory. 对于INSTALLED_APPS中的每个应用程序,加载程序将查找模板子目录。 If the directory exists, Django looks for templates in there. 如果目录存在,则Django在其中查找模板。

This means you can store templates with your individual apps. 这意味着您可以将模板与各个应用程序一起存储。 This also makes it easy to distribute Django apps with default templates. 这也使使用默认模板分发Django应用变得容易。

So you could put app-specific templates in in your app directories and keep your general templates (base.html, etc.) in the top level template dir of your project. 因此,您可以将特定于应用程序的模板放在应用程序目录中,并将常规模板(base.html等)保留在项目的顶级模板目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM