如何防止在Django模板中多次包含静态文件? [英] How to prevent static files being included multiple times in Django templates?

查看:84
本文介绍了如何防止在Django模板中多次包含静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些模板标签,其中包括标题中的一些JavaScript和CSS文件。问题是,如果我在同一个模板中多次使用这些标签,文件将被不必要地包含多次。有没有一种常规的方式来防止这种情况发生。 (C ++中的 #ifdef

解决方案

我想你需要组织你的模板,以便包含在顶层层次结构中,只有在你需要的模板中继承它。



Django提供了非常强大的机制扩展模板 https://docs.djangoproject.com/en/dev/ref/模板/内建/ ,但我不认为你实际上需要像处理器之类的守卫,你可以大部分时间在他们周围工作。



也许您需要的最实际的例子可以帮助您查看问题。


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++)

解决方案

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.

这篇关于如何防止在Django模板中多次包含静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆