Bird
0
0

Which of the following is the correct syntax to cache a template fragment for 120 seconds with key 'sidebar'?

easy📝 Syntax Q3 of 15
Django - Caching
Which of the following is the correct syntax to cache a template fragment for 120 seconds with key 'sidebar'?
A{% cache timeout=120 key='sidebar' %} ... {% endcache %}
B{% cache 'sidebar' 120 %} ... {% endcache %}
C{% cache 120 sidebar %} ... {% endcache %}
D{% cache 120 'sidebar' %} ... {% endcache %}
Step-by-Step Solution
Solution:
  1. Step 1: Recall the correct order of arguments in cache tag

    The syntax is {% cache timeout_in_seconds 'cache_key' %} followed by content and {% endcache %}.
  2. Step 2: Check each option

    {% cache 120 'sidebar' %} ... {% endcache %} matches the correct syntax. {% cache 'sidebar' 120 %} ... {% endcache %} swaps arguments. {% cache timeout=120 key='sidebar' %} ... {% endcache %} uses invalid named arguments. {% cache 120 sidebar %} ... {% endcache %} misses quotes around key.
  3. Final Answer:

    {% cache 120 'sidebar' %} ... {% endcache %} -> Option D
  4. Quick Check:

    Correct cache syntax = C [OK]
Quick Trick: Timeout first, then quoted key in cache tag [OK]
Common Mistakes:
MISTAKES
  • Swapping timeout and key order
  • Omitting quotes around key
  • Using named arguments incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes