Per-view caching helps your website load faster by saving the result of a page so it doesn't have to be created again each time someone visits.
Per-view caching in Django
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Django
from django.views.decorators.cache import cache_page @cache_page(timeout_in_seconds) def my_view(request): # view code here return HttpResponse('content')
Use the @cache_page decorator above your view function to enable caching.
The timeout is how long the cached page stays saved before it refreshes.
Examples
Django
from django.views.decorators.cache import cache_page @cache_page(60 * 15) def homepage(request): return HttpResponse('Welcome to the homepage!')
Django
from django.views.decorators.cache import cache_page @cache_page(3600) def about(request): return HttpResponse('About us page')
Django
from django.views.decorators.cache import cache_page @cache_page(0) def no_cache_view(request): return HttpResponse('No caching here')
Sample Program
This simple Django view returns a message and caches the page for 30 seconds. If you visit the page multiple times within 30 seconds, Django will serve the saved page instead of running the view again.
Django
from django.http import HttpResponse from django.views.decorators.cache import cache_page @cache_page(30) def hello_view(request): return HttpResponse('Hello, this page is cached for 30 seconds!')
Important Notes
Remember to import cache_page from django.views.decorators.cache.
Per-view caching works best for pages that do not change for all users.
To clear the cache manually, you may need to restart your server or use cache backend commands.
Summary
Per-view caching saves the output of a view to speed up repeated visits.
Use the @cache_page(timeout) decorator to enable it.
Choose a timeout that fits how often your page content changes.
Practice
1. What is the main purpose of using
@cache_page(timeout) in Django views?easy
Solution
Step 1: Understand what caching does
Caching stores the output of a view so it can be reused without recalculating.Step 2: Recognize the role of
This decorator saves the rendered page for the given timeout to speed up future requests.@cache_page(timeout)Final Answer:
To save the view's output and speed up repeated visits -> Option BQuick Check:
Per-view caching = save output for speed [OK]
Hint: Caching saves page output to speed up repeated visits [OK]
Common Mistakes:
- Thinking it validates input
- Confusing caching with authentication
- Assuming it connects to the database
2. Which of the following is the correct way to apply per-view caching with a 5-minute timeout in Django?
easy
Solution
Step 1: Understand the timeout parameter
The timeout is in seconds, so 5 minutes = 300 seconds.Step 2: Check correct syntax for
The decorator takes an integer timeout in seconds without quotes.@cache_pageFinal Answer:
@cache_page(300)\ndef my_view(request):\n pass -> Option DQuick Check:
Timeout in seconds, no quotes = @cache_page(300) def my_view(request): pass [OK]
Hint: Timeout is seconds as integer, no quotes [OK]
Common Mistakes:
- Using string '300' instead of integer
- Passing timeout as minutes instead of seconds
- Using keyword argument timeout incorrectly
3. Given this Django view with per-view caching:
What will happen if you refresh the page multiple times within 60 seconds?
@cache_page(60)
def home(request):
return HttpResponse(str(time.time()))What will happen if you refresh the page multiple times within 60 seconds?
medium
Solution
Step 1: Understand what
It caches the entire response for 60 seconds.@cache_page(60)doesStep 2: Analyze the effect on
Since the response is cached, the timestamp won't update until cache expires.time.time()Final Answer:
The page shows the same timestamp for 60 seconds -> Option AQuick Check:
Cache holds output unchanged for timeout [OK]
Hint: Cached view returns same output until timeout expires [OK]
Common Mistakes:
- Expecting timestamp to update every refresh
- Thinking caching only affects database queries
- Assuming cache raises errors with dynamic content
4. You wrote this Django view with caching:
Why does this code cause an error?
from django.views.decorators.cache import cache_page
@cache_page('60')
def about(request):
return HttpResponse('About page')Why does this code cause an error?
medium
Solution
Step 1: Check the timeout argument type
The timeout must be an integer number of seconds, not a string.Step 2: Identify the error cause
Passing '60' as a string causes a TypeError in the decorator.Final Answer:
The timeout value is a string instead of an integer -> Option AQuick Check:
Timeout must be int, not string [OK]
Hint: Timeout must be integer, not string [OK]
Common Mistakes:
- Using quotes around timeout number
- Forgetting parentheses on decorator
- Thinking function type matters here
5. You want to cache a Django view that shows user-specific data but still use per-view caching. Which approach correctly applies caching without showing wrong data to users?
hard
Solution
Step 1: Understand caching user-specific data risks
Global caching can show one user's data to others if not separated.Step 2: Use
AddingVaryheader to separate cache by user sessionVary: Cookietells cache to store different versions per user session cookie.Final Answer:
Use@cache_page(120)and addVary: Cookieheader to cache per user session -> Option CQuick Check:
Vary header separates cache by user [OK]
Hint: Use Vary header to separate cache per user [OK]
Common Mistakes:
- Assuming caching separates users automatically
- Avoiding caching user views unnecessarily
- Adding user ID in URL without proper cache keys
