How to prevent static files being included multiple times in Django templates?

◇◆丶佛笑我妖孽 提交于 2019-12-13 06:17:18

问题


I have some template tags, which include some JavaScript and CSS files in the header. The problem is, that if I use these tags multiple times in the same template, the files are included multiple times unnecessarily. Is there a conventional way to prevent this from happening. (Like #ifdef in C++)


回答1:


I think you need to organise your templates so that includes are made in the top hierarchy one, and only inherits from it in only the templates where you need it.

Django provides a very powerful mechanism to extends templates https://docs.djangoproject.com/en/dev/ref/templates/builtins/ but I don't think you actually need pre-processors-like guards, you can go most of the time work around them.

Maybe a most practical example of your need would help to see your problem.



来源:https://stackoverflow.com/questions/16100417/how-to-prevent-static-files-being-included-multiple-times-in-django-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!