elethan elethan - 8 months ago 35
HTML Question

Performance hits from loading Django static tag multiple times

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

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

And then in `templateB.html, I have:

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

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

My question is this:

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?

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
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!


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.