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?
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}")
Think about what caching does to the response within the cache timeout period.
The @cache_page(60) decorator caches the entire response for 60 seconds. So, any request within that time returns the cached response, ignoring data changes.
Choose the correct way to apply a 5-minute cache to a Django view function.
Remember the decorator syntax and optional parameters.
Option C correctly applies a 300-second cache with a key_prefix to avoid collisions. Option C is invalid syntax due to the line break. Options C and D are syntactically valid but lack the key_prefix.
A developer applies @cache_page(60) to a Django view that handles POST requests. The cache seems to never return cached responses. Why?
Think about HTTP methods and caching behavior.
Django's per-view caching only caches GET and HEAD requests by default. POST requests are not cached because they usually change data and should not be cached.
What happens if two different views use @cache_page(60) without specifying a key_prefix and have the same URL pattern?
Consider how cache keys are generated and what happens if they overlap.
Without a unique key_prefix, cache keys can collide if URL patterns are the same, causing one view's cached response to be served for the other.
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:
- Request to view1 with URL
/page/1/ - Request to view2 with URL
/page/1/ - Request to view1 with URL
/page/2/ - Request to view2 with URL
/page/1/again
How many unique cache entries exist after these requests?
Think about how cache keys combine URL and key_prefix.
Each unique combination of view (key_prefix) and URL creates a unique cache entry. Requests 1 and 3 create two entries for view1 with different URLs. Requests 2 and 4 are for view2 with the same URL, so only one cache entry for view2.