简体   繁体   中英

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.

At work, we are using a home-made custom django package that we add to installed apps in our main projects. 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).

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
  • 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" %}

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.

Is there something I'm missing in django-crispy-forms maybe? Or would loaders be a relevant/better angle and if so, how would you explain them to your neighbors' 10 year old kid? 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 has a FileSystemFinder which will search for templates in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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