多次加载Django静态代码带来的性能提升 [英] Performance hits from loading Django static tag multiple times

查看:74
本文介绍了多次加载Django静态代码带来的性能提升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除非我做错事,否则,如果您有嵌套模板(即,模板中的 {%include%} 模板),则有时需要在嵌套的多个层中调用 {%load static%} 。例如,假设我有 templateA.html

Unless I am doing things wrong, it seems like if you have nested templates (i.e., {% include %} a template within a template), you will sometimes need to call {% load static %} in multiple "layers" of the nest. For example, say I have templateA.html:

{% load static %}
<a href={% static "some/path" %}>Some Link</a>
{% include 'templateB.html' %}

然后在`templateB.html中,我有:

And then in `templateB.html, I have:

{% load static %}
<a href={% static "some/other/path" %}>Some Other Link</a>

据测试可知,我必须包含 {%load在两个模板中都是static%} ,因为 templateB.html 不知道我已经加载了 {%static% } 标签。

As far as I can tell from testing, I must include {% load static %} in both templates, because templateB.html does not know that I have already loaded the {% static %} tag.

我的问题是这样:

假设有必要两次加载 {%static%} 标记(或更多次,具体取决于嵌套量),这种额外的加载是否会对性能造成影响?

Assuming that it is necessary to load the {% static %} tag twice (or more times depending on the amount of nesting), is there going to be a performance hit from this extra loading?

我不确定在加载此标记时Django会在后台执行什么操作,但是我的直觉是您不想加载和重新加载静态文件。 (由于我们正在谈论一个开源项目,实际上我做了 try 来亲自研究这个 templatetag 是如何实现的,但是

I am not sure what Django does under the hood when you load this tag, but my intuition is that you don't want to be loading and reloading static files. (Since we are talking about an open source project, I did actually try to look under the hood myself at how this templatetag is implemented, but it proved to be a little beyond my comprehension...).

此外,此问题假定有必要始终以这种方式加载标签。如果我缺少某些东西,我将非常有兴趣了解更多信息。谢谢!

Also, this question assumes that it is necessary to always load the tag this way. If there is something I am missing, I would be very interested to learn more. Thank you!

推荐答案

没有任何开销。 加载静态文件不会加载和重新加载静态文件;只是在staticfiles templatetags库中提供了(已加载的)代码,供您在模板中使用。

There is no overhead. load static does not "load and reload static files"; it just makes available the (already-loaded) code in the staticfiles templatetags library for use in your template.

这篇关于多次加载Django静态代码带来的性能提升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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