Bird
0
0

How can you combine per-view caching with user authentication to cache pages differently for anonymous and logged-in users?

hard📝 Application Q9 of 15
Django - Caching
How can you combine per-view caching with user authentication to cache pages differently for anonymous and logged-in users?
AUse @login_required with cache_page
BUse vary_on_cookie decorator along with cache_page
CSet timeout to zero for logged-in users
DCache only anonymous users by removing cache_page for logged-in
Step-by-Step Solution
Solution:
  1. Step 1: Understand caching with user sessions

    Logged-in users have cookies; anonymous users do not.
  2. Step 2: Use vary_on_cookie to differentiate cache

    Applying vary_on_cookie makes cache keys vary by cookie presence, so logged-in and anonymous get separate caches.
  3. Final Answer:

    Use vary_on_cookie decorator along with cache_page -> Option B
  4. Quick Check:

    vary_on_cookie separates cache by user login state [OK]
Quick Trick: Use vary_on_cookie to cache differently by login status [OK]
Common Mistakes:
MISTAKES
  • Assuming login_required affects caching
  • Setting timeout zero disables caching entirely
  • Removing cache_page for logged-in users manually

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes