Bird
0
0

Why does this cached view not cache responses?

medium📝 Debug Q7 of 15
Django - Caching
Why does this cached view not cache responses?
@cache_page(60)
def view(request, id):
    return HttpResponse(f"ID: {id}")
ABecause the view has a parameter, cache key must include it
BBecause @cache_page only works on class-based views
CBecause HttpResponse cannot be cached
DBecause timeout is too short
Step-by-Step Solution
Solution:
  1. Step 1: Understand cache key and view parameters

    When a view has parameters, the cache key must include them to cache different outputs.
  2. Step 2: Evaluate other options

    @cache_page works on function views, HttpResponse is cacheable, timeout 60 seconds is valid.
  3. Final Answer:

    Because the view has a parameter, cache key must include it -> Option A
  4. Quick Check:

    Cache key must vary by view parameters [OK]
Quick Trick: Cache keys must include view args to cache varied responses [OK]
Common Mistakes:
MISTAKES
  • Thinking @cache_page only works on class views
  • Believing HttpResponse can't be cached
  • Assuming timeout length disables caching

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes