[Django]-Performance hits from loading Django static tag multiple times

2👍

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.

4👍

You have to write the tag in every template. In case of performance, you need not to worry as it never reloads or loads a separate new copy of static files.

2👍

By using load you adding tags and filters from some app into the context for the current template. It just calls parser.add_library() for parser and updates the list of tags and filters for this particular template. You can check this method, and it gets called from load tag
If you don’t want to load something you can add it in the builtins. For Django 1.9 you can configure it like this

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'builtins': ['django.templatetags.static'],
        },
    },
]

and for older versions

from django.template.loader import add_to_builtins
add_to_builtins('django.templatetags.static')

Leave a comment