简体   繁体   English

为 django 包添加模板文件夹选择

[英]Add a template folder selection for a django package

I am relatively new to django and am looking for help on custom package templates management.我对 django 比较陌生,正在寻找有关自定义包模板管理的帮助。

At work, we are using a home-made custom django package that we add to installed apps in our main projects.在工作中,我们使用自制的自定义 django 包,我们将其添加到主要项目中已安装的应用程序中。 This package has tools and utilities, as well as the base templates that we derive from in main projects.这个包有工具和实用程序,以及我们在主要项目中派生的基本模板。

What's changing?有什么变化? We need to update our frontend and are changing libraries, which has an impact on the templates in this custom package.我们需要更新我们的前端并且正在更改库,这对这个自定义包中的模板有影响。

With a view to avoid multiplying the custom package branches and versions (we already have a couple for different versions of django), and given that the templates in our existing applications will not all be updated at once, we wish to make it so that the main project will use either the old templates or the new version of templates (variable selection or default).为了避免增加自定义包分支和版本(我们已经有几个用于不同版本的 django),并且考虑到我们现有应用程序中的模板不会一次全部更新,我们希望这样做主项目将使用旧模板或新版本的模板(变量选择或默认)。

What I aim for (if possible):我的目标(如果可能的话):

  • adding a folder level in the custom package templates folder that the project will look in在项目将在其中查找的自定义包模板文件夹中添加文件夹级别
  • adding a variable (in the project's settings.py file for example) to indicate which template folder the main project needs to look for in the package添加一个变量(例如在项目的 settings.py 文件中)以指示主项目需要在包中查找哪个模板文件夹
  • not having to modify the old templates folder in the package, or the old templates in the applications.不必修改包中的旧模板文件夹或应用程序中的旧模板。 For example, having to do a critical update related to security in this package should not require updating all templates in an existing project based on the old templates).例如,必须在此包中进行与安全相关的关键更新,不应该要求基于旧模板更新现有项目中的所有模板)。

I've looked into how the django-crispy-forms package does its selection of the bootstrap version but from what I understand it systematically renders a view of an item giving the precision of the template pack, which seems to me would require a huge overall as our base application templates derive from the custom package templates with a simple {% extends "custom_package/templatename.html" %}我已经研究了 django-crispy-forms 包如何选择引导程序版本,但据我所知,它系统地呈现了一个项目的视图,给出了模板包的精度,在我看来,这需要一个巨大的整体因为我们的基本应用程序模板从自定义包模板派生而来,带有一个简单的 {% extends "custom_package/templatename.html" %}

I've also tried to understand django loaders but I'll admit I'm not sure this is the right way either, and in spite of reading the django documentation on this, I still find them a bit cryptic and do not know how I would go about it for our specific purpose.我也试图了解 django 加载器,但我承认我也不确定这是否是正确的方法,尽管阅读了有关此的 django 文档,我仍然觉得它们有点神秘,不知道我是怎么做的会为了我们的特定目的而去做。

Is there something I'm missing in django-crispy-forms maybe?也许我在 django-crispy-forms 中遗漏了什么? Or would loaders be a relevant/better angle and if so, how would you explain them to your neighbors' 10 year old kid?或者装载机是否是一个相关/更好的角度,如果是这样,您将如何向邻居的 10 岁孩子解释它们? Is there another option you might have in mind that would help achieve our goal?您是否有其他选择可以帮助实现我们的目标?

Any help is appreciated!任何帮助表示赞赏!

If you want django to use your templates whenever possible and use templates with the same name whenever needed, this answer will be of help.如果您希望 django 尽可能使用您的模板并在需要时使用同名模板,则此答案将有所帮助。

Django has a FileSystemFinder which will search for templates in STATICFILES_DIRS . Django 有一个 FileSystemFinder,它将在STATICFILES_DIRS搜索模板。 If you register your apps before other apps in STATICFILES_DIR, django will find the templates of your app first and use them whenever possible.如果您在 STATICFILES_DIR 中先注册您的应用程序,则 django 将首先找到您的应用程序模板并尽可能使用它们。

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

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