Skip to content

Commit 2766e20

Browse files
committed
How to add Template Fragment Caching
1 parent 49a73be commit 2766e20

5 files changed

Lines changed: 60 additions & 43 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*.egg*
44
local.py
55
.mypy_cache
6+
*.djcache
67

78
# Temporary files
89
*.swp

cache/.gitkeep

Whitespace-only changes.

mysite/settings/dev.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222

2323
INTERNAL_IPS = ("127.0.0.1", "172.17.0.1")
2424

25+
# Uncomment this line to enable template caching
26+
# Dont forget to change the LOCATION path
27+
# CACHES = {
28+
# "default": {
29+
# "BACKEND": "django.core.cache.backends.filebased.FileBasedCache",
30+
# "LOCATION": "/path/to/your/site/cache"
31+
# }
32+
# }
33+
34+
2535
try:
2636
from .local import *
2737
except ImportError:

mysite/templates/base.html

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% load static wagtailuserbar menus_tags %}
1+
{% load static wagtailuserbar menus_tags cache %}
22

33
{% get_menu "main" as navigation %}
44

@@ -45,11 +45,13 @@
4545
<a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
4646
</li>
4747

48-
{% for item in navigation.menu_items.all %}
49-
<li>
50-
<a href="{{ item.link }}" class="nav-link"{% if item.open_in_new_tab %} target="_blank"{% endif %}>{{ item.title }}</a>
51-
</li>
52-
{% endfor %}
48+
{% cache 604800 navigation %}
49+
{% for item in navigation.menu_items.all %}
50+
<li>
51+
<a href="{{ item.link }}" class="nav-link"{% if item.open_in_new_tab %} target="_blank"{% endif %}>{{ item.title }}</a>
52+
</li>
53+
{% endfor %}
54+
{% endcache %}
5355
</ul>
5456
<!--
5557
<form class="form-inline my-2 my-lg-0">
@@ -63,27 +65,29 @@
6365

6466
{% block content %}{% endblock %}
6567

66-
<div class="container">
67-
<div class="row">
68-
<div class="col-lg-12 text-center">
69-
{% if settings.site_settings.SocialMediaSettings.facebook %}
70-
<a href="{{ settings.site_settings.SocialMediaSettings.facebook }}">
71-
<i class="fab fa-facebook-f"></i>
72-
</a>
73-
{% endif %}
74-
{% if settings.site_settings.SocialMediaSettings.twitter %}
75-
<a href="{{ settings.site_settings.SocialMediaSettings.twitter }}">
76-
<i class="fab fa-twitter"></i>
77-
</a>
78-
{% endif %}
79-
{% if settings.site_settings.SocialMediaSettings.youtube %}
80-
<a href="{{ settings.site_settings.SocialMediaSettings.youtube }}">
81-
<i class="fab fa-youtube"></i>
82-
</a>
83-
{% endif %}
68+
{% cache 604800 footer %}
69+
<div class="container">
70+
<div class="row">
71+
<div class="col-lg-12 text-center">
72+
{% if settings.site_settings.SocialMediaSettings.facebook %}
73+
<a href="{{ settings.site_settings.SocialMediaSettings.facebook }}">
74+
<i class="fab fa-facebook-f"></i>
75+
</a>
76+
{% endif %}
77+
{% if settings.site_settings.SocialMediaSettings.twitter %}
78+
<a href="{{ settings.site_settings.SocialMediaSettings.twitter }}">
79+
<i class="fab fa-twitter"></i>
80+
</a>
81+
{% endif %}
82+
{% if settings.site_settings.SocialMediaSettings.youtube %}
83+
<a href="{{ settings.site_settings.SocialMediaSettings.youtube }}">
84+
<i class="fab fa-youtube"></i>
85+
</a>
86+
{% endif %}
87+
</div>
8488
</div>
8589
</div>
86-
</div>
90+
{% endcache %}
8791

8892
{# Global javascript #}
8993
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

mysite/templates/blog/blog_listing_page.html

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% extends "base.html" %}
22

3-
{% load wagtailimages_tags wagtailroutablepage_tags %}
3+
{% load wagtailimages_tags wagtailroutablepage_tags cache %}
44

55
{% block content %}
66

@@ -19,25 +19,27 @@ <h2>
1919

2020
<div class="container">
2121
{% for post in posts %}
22-
<div class="row mt-5 mb-5">
23-
<div class="col-sm-3">
24-
{% image post.banner_image fill-250x250 as blog_img %}
25-
<a href="{{ post.url }}">
26-
<img src="{{ blog_img.url }}" alt="{{ blog_img.alt }}" style='width: 100%;'>
27-
</a>
28-
</div>
29-
<div class="col-sm-9">
30-
<a href="{{ post.url }}">
31-
<h2>{{ post.custom_title }}</h2>
32-
{% if post.specific.subtitle %}
33-
<p>{{ post.specific.subtitle }}</p>
34-
{% endif %}
22+
{% cache 604800 blog_post_preview post.id %}
23+
<div class="row mt-5 mb-5">
24+
<div class="col-sm-3">
25+
{% image post.banner_image fill-250x250 as blog_img %}
26+
<a href="{{ post.url }}">
27+
<img src="{{ blog_img.url }}" alt="{{ blog_img.alt }}" style='width: 100%;'>
28+
</a>
29+
</div>
30+
<div class="col-sm-9">
31+
<a href="{{ post.url }}">
32+
<h2>{{ post.custom_title }}</h2>
33+
{% if post.specific.subtitle %}
34+
<p>{{ post.specific.subtitle }}</p>
35+
{% endif %}
3536

36-
{# @todo add a summary field to BlogDetailPage; make it a RichTextField with only Bold and Italic enabled. #}
37-
<a href="{{ post.url }}" class="btn btn-primary mt-4">Read More</a>
38-
</a>
37+
{# @todo add a summary field to BlogDetailPage; make it a RichTextField with only Bold and Italic enabled. #}
38+
<a href="{{ post.url }}" class="btn btn-primary mt-4">Read More</a>
39+
</a>
40+
</div>
3941
</div>
40-
</div>
42+
{% endcache %}
4143
{% endfor %}
4244
</div>
4345

0 commit comments

Comments
 (0)