0
0
Djangoframework~20 mins

Template fragment caching in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Fragment Caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Django template fragment cache do?
Consider this Django template code:
{% 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 %}
ACaches the sidebar content for 600 seconds separately for each user based on their user ID.
BCaches the sidebar content globally for 600 seconds, ignoring user differences.
CCaches the sidebar content indefinitely until manually cleared.
DDoes not cache anything because the cache tag is missing a required argument.
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.
📝 Syntax
intermediate
2: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 %}
AThe cache timeout should be an integer without quotes, not a string.
BThe endcache tag is missing a required argument.
CThe cache tag requires at least one variable argument after the key.
DThe cache key 'menu' should not be quoted.
Attempts:
2 left
💡 Hint
Check the type of the timeout argument in the cache tag.
🔧 Debug
advanced
2:00remaining
Why does this fragment cache not update after the data changes?
Given this template fragment cache:
{% 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 %}
AThe products variable is not passed to the template, so the cache is empty.
BThe cache key 'product_list' is static, so the cache never invalidates when products change.
CThe cache timeout of 900 seconds is too short to see updates.
DThe cache tag requires a user ID to differentiate cache entries.
Attempts:
2 left
💡 Hint
Think about how the cache key affects cache invalidation.
state_output
advanced
2:00remaining
What is the output count of cached items with this fragment cache?
If this template fragment cache is used:
{% 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 %}
A2 cache entries (one per user, ignoring posts).
B3 cache entries (one per post, ignoring users).
C6 cache entries (3 posts × 2 users).
D1 cache entry shared by all posts and users.
Attempts:
2 left
💡 Hint
The cache key includes both post.id and user.id.
🧠 Conceptual
expert
2: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?
AIt requires no cache key and thus is simpler to implement than other caching methods.
BIt caches the entire page content, improving performance for all users equally.
CIt automatically updates cached content whenever the database changes without extra code.
DIt allows caching parts of a page that are expensive to render while keeping other parts dynamic and personalized.
Attempts:
2 left
💡 Hint
Think about when you want some parts of a page to update often and others to stay fast.