如何设置304 http响应代码的Content-Type标头?

时间:2013-02-12 09:42:07

标签: python django

所以我使用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响应代码返回什么内容类型?

1 个答案:

答案 0 :(得分: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
相关问题