Hint: Timeout first, then quoted key in cache tag [OK]
Common Mistakes:
Putting key before timeout
Not quoting the cache key string
Using variable name without quotes
3. Given this template code:
{% cache 600 'menu' user.id %}
Home
Profile
{% endcache %} What happens if user.id changes?
medium
A. A new cache entry is created for the new user.id
B. The cached fragment is reused regardless of user.id
C. The cache is cleared completely
D. An error occurs because user.id cannot be used
Solution
Step 1: Understand cache key with extra parameters
Extra parameters after the key string are used to create a unique cache key per value.
Step 2: Effect of changing user.id on cache
When user.id changes, Django creates a new cache entry for that user, so the fragment is cached separately.
Final Answer:
A new cache entry is created for the new user.id -> Option A
Quick Check:
Cache key + params = unique cache per user [OK]
Hint: Extra params create unique cache keys [OK]
Common Mistakes:
Assuming cache ignores extra parameters
Thinking cache clears all entries on param change
Believing user.id cannot be used in cache tag
4. Identify the error in this template fragment caching usage:
{% cache 'sidebar' 300 %}
Sidebar content
{% endcache %}
medium
A. The timeout and key order is reversed; timeout must come first
B. Missing closing tag {% endcache %}
C. The cache key must be an integer, not a string
D. Cache tag cannot wrap HTML elements
Solution
Step 1: Check the order of arguments in the cache tag
The correct order is timeout (integer) first, then cache key (string).
Step 2: Identify the mistake in the given code
The code uses 'sidebar' first and 300 second, which is reversed.
Final Answer:
The timeout and key order is reversed; timeout must come first -> Option A
Quick Check:
Timeout first, key second in cache tag [OK]
Hint: Timeout always before key in cache tag [OK]
Common Mistakes:
Swapping timeout and key order
Forgetting to close cache tag
Thinking cache tag can't wrap HTML
5. You want to cache a sidebar that shows user-specific data but also updates every 10 minutes. Which is the best way to use template fragment caching?
hard
A. Do not use cache because user data changes
B. {% cache 600 'sidebar' %} ... {% endcache %} to cache once for all users
C. {% cache 600 'sidebar' user.id %} ... {% endcache %} to cache per user for 10 minutes
D. {% cache 'sidebar' 600 user.id %} ... {% endcache %} with wrong argument order
Solution
Step 1: Understand caching user-specific data
To cache user-specific content, include a unique user identifier in the cache key.
Step 2: Set cache timeout to 600 seconds (10 minutes)
Use 600 seconds as timeout to update cache every 10 minutes.
Step 3: Verify correct syntax and usage
{% cache 600 'sidebar' user.id %} ... {% endcache %} to cache per user for 10 minutes uses correct syntax with timeout first, key string second, and user.id as extra parameter.
Final Answer:
{% cache 600 'sidebar' user.id %} ... {% endcache %} to cache per user for 10 minutes -> Option C
Quick Check:
User-specific cache with timeout = {% cache 600 'sidebar' user.id %} ... {% endcache %} to cache per user for 10 minutes [OK]
Hint: Use user.id param and timeout for user-specific cache [OK]