【问题标题】:Can the results from the Wagtail API be cached or have a `Cache-Control` header set?Wagtail API 的结果是否可以缓存或设置“Cache-Control”标头?
【发布时间】:2018-09-13 20:31:49
【问题描述】:
【问题讨论】:
标签:
django-rest-framework
wagtail
【解决方案1】:
Wagtail API 端点在 wagtail.api.v2.endpoints(以及其他位置,例如 wagtail.images.api.v2.endpoints)中定义,并且可以进行子类化以提供自定义行为,例如在响应中设置额外的标头。例如,将 Cache-Control 标头添加到PagesAPIEndpoint的详细视图:
from wagtail.api.v2.endpoints import PagesAPIEndpoint
class CachedPagesAPIEndpoint(PagesAPIEndpoint):
def detail_view(self, request, pk):
response = super().detail_view(request, pk)
response['Cache-Control'] = 'no-cache'
return response
然后,in your api.py,注册您的自定义 CachedPagesAPIEndpoint 代替标准 PagesAPIEndpoint。