Challenge - 5 Problems
Template Fragment Caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Django template fragment cache do?
Consider this Django template code:
What is the effect of this fragment cache?
{% load cache %}
{% cache 600 sidebar user.id %}
<div>User sidebar content</div>
{% endcache %}What is the effect of this fragment cache?
Django
{% load cache %}
{% cache 600 sidebar user.id %}
<div>User sidebar content</div>
{% endcache %}Attempts:
2 left
💡 Hint
Look at the cache tag arguments: the first is time in seconds, the second is a cache key prefix, the third is a variable.
✗ Incorrect
The cache tag caches the content for 600 seconds. The cache key includes 'sidebar' and the current user's ID, so each user gets their own cached version.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Django template fragment cache usage
What is wrong with this Django template fragment cache code?
{% load cache %}
{% cache '300' 'menu' %}
<nav>Menu content</nav>
{% endcache %}Django
{% load cache %}
{% cache '300' 'menu' %}
<nav>Menu content</nav>
{% endcache %}Attempts:
2 left
💡 Hint
Check the type of the timeout argument in the cache tag.
✗ Incorrect
The timeout argument must be an integer, not a string. Using quotes makes it a string, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this fragment cache not update after the data changes?
Given this template fragment cache:
When the products list changes, the cached content does not update. Why?
{% load cache %}
{% cache 900 product_list %}
<ul>
{% for product in products %}
<li>{{ product.name }}</li>
{% endfor %}
</ul>
{% endcache %}When the products list changes, the cached content does not update. Why?
Django
{% load cache %}
{% cache 900 product_list %}
<ul>
{% for product in products %}
<li>{{ product.name }}</li>
{% endfor %}
</ul>
{% endcache %}Attempts:
2 left
💡 Hint
Think about how the cache key affects cache invalidation.
✗ Incorrect
Because the cache key is fixed as 'product_list', the cached content stays the same for 900 seconds regardless of product changes. To update cache on data change, the key must include a variable that changes when data changes.
❓ state_output
advanced2:00remaining
What is the output count of cached items with this fragment cache?
If this template fragment cache is used:
And there are 3 posts and 2 users, how many separate cache entries will be created?
{% load cache %}
{% cache 120 comments post.id user.id %}
<div>Comments section</div>
{% endcache %}And there are 3 posts and 2 users, how many separate cache entries will be created?
Django
{% load cache %}
{% cache 120 comments post.id user.id %}
<div>Comments section</div>
{% endcache %}Attempts:
2 left
💡 Hint
The cache key includes both post.id and user.id.
✗ Incorrect
The cache key is built from 'comments', post.id, and user.id. With 3 posts and 2 users, the combinations are 3 × 2 = 6 unique keys, so 6 cache entries.
🧠 Conceptual
expert2:00remaining
What is the main advantage of using template fragment caching in Django?
Why would a developer choose to use template fragment caching instead of full-page caching in Django?
Attempts:
2 left
💡 Hint
Think about when you want some parts of a page to update often and others to stay fast.
✗ Incorrect
Template fragment caching lets you cache only slow parts of a page, so you can keep other parts fresh and personalized, unlike full-page caching which caches everything.