Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.

Solution 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.

Solution 3:

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

Post a Comment for "Performance Hits From Loading Django Static Tag Multiple Times"