问题
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