0
0
Djangoframework~20 mins

Per-view caching in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Per-view Caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of a cached Django view after data changes?

Consider a Django view decorated with @cache_page(60). The view returns the current time. If the underlying data changes immediately after the first request, what will the second request within 60 seconds show?

Django
from django.views.decorators.cache import cache_page
from django.http import HttpResponse
import datetime

@cache_page(60)
def current_time(request):
    now = datetime.datetime.now()
    return HttpResponse(f"Current time: {now}")
AThe second request returns an empty response due to caching.
BThe second request raises a cache miss error because data changed.
CThe second request shows the updated current time reflecting the data change.
DThe second request shows the same time as the first request because the response is cached for 60 seconds.
Attempts:
2 left
💡 Hint

Think about what caching does to the response within the cache timeout period.

📝 Syntax
intermediate
2:00remaining
Which decorator syntax correctly applies per-view caching in Django?

Choose the correct way to apply a 5-minute cache to a Django view function.

A
@cache_page(5*60)
def my_view(request):
    return HttpResponse('Hello')
B
@cache_page
(300)
def my_view(request):
    return HttpResponse('Hello')
C
@cache_page(300, key_prefix='myprefix')
def my_view(request):
    return HttpResponse('Hello')
D
@cache_page(300)
def my_view(request):
    return HttpResponse('Hello')
Attempts:
2 left
💡 Hint

Remember the decorator syntax and optional parameters.

🔧 Debug
advanced
2:00remaining
Why does per-view caching not work for a Django view with POST requests?

A developer applies @cache_page(60) to a Django view that handles POST requests. The cache seems to never return cached responses. Why?

APer-view caching only caches GET and HEAD requests by default, so POST requests are never cached.
BPOST requests are cached but require a different decorator.
CThe cache backend does not support POST request caching.
DThe decorator must be applied after the view function, not before.
Attempts:
2 left
💡 Hint

Think about HTTP methods and caching behavior.

🧠 Conceptual
advanced
2:00remaining
How does the cache key affect per-view caching in Django?

What happens if two different views use @cache_page(60) without specifying a key_prefix and have the same URL pattern?

ABoth views share the same cache key, causing cached responses to be mixed up.
BDjango automatically generates unique keys per view, so no conflict occurs.
CThe cache keys collide only if the views have the same function name.
DCaching is disabled if key_prefix is not specified.
Attempts:
2 left
💡 Hint

Consider how cache keys are generated and what happens if they overlap.

state_output
expert
2:00remaining
What is the number of cache entries after these requests with per-view caching?

Given two Django views, both decorated with @cache_page(120, key_prefix='view1') and @cache_page(120, key_prefix='view2') respectively, and the following sequence of requests:

  1. Request to view1 with URL /page/1/
  2. Request to view2 with URL /page/1/
  3. Request to view1 with URL /page/2/
  4. Request to view2 with URL /page/1/ again

How many unique cache entries exist after these requests?

A1 unique cache entry
B3 unique cache entries
C2 unique cache entries
D4 unique cache entries
Attempts:
2 left
💡 Hint

Think about how cache keys combine URL and key_prefix.