1

所以我使用 Django 中的一些装饰器来启用我的 API 上的缓存:

@require_GET
@cache_page(100)
@cache_control(max_age=100, s_maxage=100)
@csrf_exempt
def my_api(request):

问题是,304 Not Modified 响应带有 text/html Content-Type 标头。我的 API 通常返回 application/json Content-Type 标头,我希望保持一致。有没有办法告诉 Django 使用 304 响应代码返回什么内容类型?

4

1 回答 1

0

问题在这里https://github.com/django/django/blob/master/django/http/response.py#L411

写一个装饰器再次添加 mimetype

def RestoreMime(fn):
  def Wrapper(*args, **kwds):
    response = fn(*args, **kwds)
    response['Content-type'] = your_mime_type
    return response
  return Wrapper
于 2013-04-11T09:23:33.230 回答