Discover how saving a page once can make your whole website feel lightning fast!
Why Per-view caching in Django? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine your website has a popular page that many visitors load every second. Each time, your server runs all the code to build that page from scratch.
Building the same page repeatedly wastes time and server power. It makes your site slower and can cause delays or crashes when many users visit at once.
Per-view caching saves the finished page the first time it is made. Later visitors get the saved page instantly, without repeating all the work.
def view(request): data = expensive_database_call() return render(request, 'page.html', {'data': data})
from django.views.decorators.cache import cache_page @cache_page(60 * 15) def view(request): data = expensive_database_call() return render(request, 'page.html', {'data': data})
It makes your website faster and more reliable by reusing ready pages instead of rebuilding them every time.
A news site caches its homepage for 15 minutes so thousands of readers get instant access without slowing the server.
Manual page building repeats costly work for every visitor.
Per-view caching stores the page output to reuse it quickly.
This improves speed, reduces server load, and handles more users smoothly.
Practice
@cache_page(timeout) in Django views?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]
- Thinking it validates input
- Confusing caching with authentication
- Assuming it connects to the database
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]
- Using string '300' instead of integer
- Passing timeout as minutes instead of seconds
- Using keyword argument timeout incorrectly
@cache_page(60)
def home(request):
return HttpResponse(str(time.time()))What will happen if you refresh the page multiple times within 60 seconds?
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]
- Expecting timestamp to update every refresh
- Thinking caching only affects database queries
- Assuming cache raises errors with dynamic content
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?
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]
- Using quotes around timeout number
- Forgetting parentheses on decorator
- Thinking function type matters here
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]
- Assuming caching separates users automatically
- Avoiding caching user views unnecessarily
- Adding user ID in URL without proper cache keys
