Evgenii Legotckoi
Evgenii LegotckoiMarch 19, 2023, 2:09 a.m.

Django - Lesson 060. Speeding up a website with caching templates

One of the ways to significantly speed up the speed of a Django site is to cache both individual parts of the site templates and cache the templates after they are compiled by the site. Therefore, we will study both of these ways to improve the speed of the site, in addition to the way we already know the correct optimization of queries to the Django database . You can still test the effectiveness of improvements using the django-silk battery, which is described in the article on improving database queries.

Now let's look at options for using caching.

Caching parts of templates

In cases where you have generated parts of the templates, such as the footer of the site (Footer) or sidebars (SideBar), then it is possible to use caching of these parts of the site. For example, I not only cached them, but even the site's Navigation Drawer and top navigation bar. This was done because I use url embeddable tags and trans translation tags to generate navigation links, which are a lot of types, and together they add a decent load to the site. Not to mention that I use dynamically configurable widgets (you can read about this in the article on the polymorphic system of dynamic widgets . And it is these widgets that each load can double or even triple the time it takes to generate a site page, and given that the types of widgets can be very different, then it will not be possible to write a reasonably efficient database query.Therefore, it is easiest to code these parts of the site template.

It will look like this

{% load i18n cache %}
{% get_current_language as LANGUAGE_CODE %}
{% cache 6000 sidebar LANGUAGE_CODE %}
  {% load sidebar sidebar_sticky from evileg_widgets %}
  {% sidebar %}
  {% sidebar_sticky %}
{% endcache %}

This code uses the embeddable cache tag, which is passed the cache key sidebar , as well as the language code LANGUAGE_CODE as an additional parameter. Language code is required to support multilingual site.

In this case, with dynamic widgets, an important nuance arises, namely, cache invalidation if the SideBar with dynamic widgets was changed through the site's administrative panel.

So, it's implemented like this:

# -*- coding: utf-8 -*-

from django.conf import settings
from django.core.cache import cache
from django.db import models
from django.core.cache.utils import make_template_fragment_key
from django.db.models.signals import post_save, post_delete
from solo.models import SingletonModel
# Some another imports

class SideBar(SingletonModel):
    # Some code of model

class Widget(models.Model):
    # Some code of model

def invalidate_cache(**kwargs):
    for code, description in settings.LANGUAGES:
        cache.delete(make_template_fragment_key('sidebar', [code]))

post_save.connect(invalidate_cache, sender=SideBar)
post_save.connect(invalidate_cache, sender=Widget)
post_delete.connect(invalidate_cache, sender=Widget)

As you can see from the code, a system of signals is used here, which, by the way, is very similar to the [signals and slots in Qt] system (https://evileg.com/en/post/87/), which I really liked as a Qt developer.

So, this system of signals and slots invalidates the cache if both the SideBar object itself and in one of the widgets have been changed. Moreover, invalidation occurs immediately for all languages. Thus, heavy database queries occur no more than once every 6000 seconds. Which is perfectly acceptable.

Cached template loader

And the next way to improve site performance is to use a cached template loader. The fact is that Django usually looks for templates every time a site page is accessed, but if you set up caching on the template loader, then site performance can double.

For example, after enabling this option, the time for generating some pages of the site decreased from 220-240 ms to 120-130 ms. Of course, this is taking into account many other tweaks to improve performance. However, the result is very good.

And the setting of this functionality is done in the settigns.py file and it should look like this.

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [ os.path.join(BASE_DIR, 'templates') ],
        'OPTIONS': {
            # some another options
            'loaders': [
                ('django.template.loaders.cached.Loader', [


It is highly recommended to cache parts of the template, even if it's just calls to url or trans tags, in a global perspective, this can affect the quality of your site for search engines. It is not for nothing that there are so many articles on the Internet that say that search engines increase very fast sites in the search results.

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!


Only authorized users can post comments.
Please, Log in or Sign up

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:60points,
  • Rating points-1

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:50points,
  • Rating points-4

C++ - Test 001. The first program and data types

  • Result:73points,
  • Rating points1
Last comments
JonnyJoMarch 30, 2023, 8:57 a.m.
Qt/C++ - Lesson 021. The drawing mouse in Qt Евгений, здравствуйте! Только начал изучение Qt и возник вопрос по 21ому уроку. После написания кода, выдаёт следующие ошибки В чём может быть проблема?
Алексей НиколаевMarch 26, 2023, 6:10 a.m.
Qt/C++ - Lesson 042. PopUp notification in the Gnome style using Qt Добрый день, взял за основу ваш PopUp notification , и немного доработал его под свои нужды. Добавил в отдельном eventloop'e всплывающую очередь уведомлений с анимацией и таймеро…
Алексей НиколаевMarch 26, 2023, 6:04 a.m.
Qt/C++ - Lesson 042. PopUp notification in the Gnome style using Qt Включите прозрачность в композит менеджере fly-admin-theme : fly-admin-theme ->Эффекты и всё заработает.
NSProjectMarch 24, 2023, 11:35 a.m.
Django - Lesson 062. How to write a block-template tabbar tag like the blocktranslate tag Да не я так к примеру просто написал.
Evgenii Legotckoi
Evgenii LegotckoiMarch 24, 2023, 7:09 a.m.
Django - Lesson 062. How to write a block-template tabbar tag like the blocktranslate tag Почитайте эту статью про "хлебные крошки"
Now discuss on the forum
BlinCTApril 1, 2023, 2:16 a.m.
Нужен совет по работе с ListView и несколькими моделями Спасибо, сейчас займусь этим.
NSProjectMarch 30, 2023, 11:55 p.m.
Проверка комментария принадлежит он пользователю или нет DRF (Django Rest Framework) Здравствуйте! Сегодня я столкнулся с такой проблеммой. Существует модель комметариев. Где их соответственно достаточное количество. Все они выводятся при помощи запроса ajax (axios). Так ка…
PisychMarch 29, 2023, 11:50 p.m.
Как подсчитать количество по условию? Да! Вот так работает! Огромное Вам спасибо! ........
Evgenii Legotckoi
Evgenii LegotckoiMarch 29, 2023, 1:11 a.m.
Замена поля ManyToMany Картинки точно нужно хранить в медиа директории на сервере, а для обращения использовать ImageField. Который будет хранить только путь к изображению на сервере. Хранить изображения в базе данных…
Виталий АнисимовJan. 29, 2023, 12:17 p.m.
Как добавить виртуальную клавиатура с Т9 в своей проект на QML. Добрый день. Прошу помочь, пишу небольше приложение в Qt. Добвил в свой проект виртуальную клавиатуру от Qt. Но как добавить в него возможность изменения Т9 никак не могу понять.

Follow us in social networks