Bird
0
0

You want to cache a view but exclude caching when a query parameter 'nocache' is present. How can you implement this?

hard📝 Application Q8 of 15
Django - Caching
You want to cache a view but exclude caching when a query parameter 'nocache' is present. How can you implement this?
ASet timeout=0 in @cache_page when 'nocache' is present
BWrite a custom decorator that skips caching if 'nocache' in request.GET
CUse @cache_page with a negative timeout
DAdd @never_cache decorator to the view
Step-by-Step Solution
Solution:
  1. Step 1: Understand conditional caching needs

    Standard @cache_page does not support skipping cache based on request data.
  2. Step 2: Choose correct approach

    Writing a custom decorator to check request.GET and skip caching is the correct method. timeout=0 disables caching always, negative timeout invalid, @never_cache disables caching always.
  3. Final Answer:

    Write a custom decorator that skips caching if 'nocache' in request.GET -> Option B
  4. Quick Check:

    Conditional caching requires custom decorator [OK]
Quick Trick: Use custom decorator to skip caching conditionally [OK]
Common Mistakes:
MISTAKES
  • Trying to use timeout=0 dynamically
  • Using negative timeout values
  • Misusing @never_cache which disables caching always

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes