【问题标题】:Using django-piston, how can I write out HTTP headers in the response?使用 django-piston,如何在响应中写出 HTTP 标头?
【发布时间】:2011-02-02 04:41:12
【问题描述】:

如何在对 django-piston 调用的响应中包含 HTTP 标头,例如 Cache-Control 或 Last-Modified?

【问题讨论】:

    标签: django django-piston


    【解决方案1】:

    您可以按照 Django 文档中specifying per view cache in urlconf 指南中的过程将其包装在您的urls.py 中。在我的情况下,我将 Piston API 放在一个单独的模块中,并且更喜欢使用 Varnish 而不是内置的 Django 缓存框架,所以我在我的api/urls.py(我的主要urls.py 包括)中使用了这种方法来设置缓存控制我想要的标题:

    from django.views.decorators.cache import cache_control
    
    cached_resource = cache_control(public=True, maxage=30, s_maxage=300)
    
    urlpatterns = patterns('',
       url(r'^myresource/$', cached_resource(Resource(MyHandler))),
    )
    

    【讨论】:

      【解决方案2】:

      不确定 django-piston,但在 django 中你可以去:

      from django.http import HttpResponse
      response = HttpResponse('My content')
      response['MyHttpHeader'] = 'MyHeaderValue'
      

      所以,在您可以访问响应的地方执行此操作。如果您使用的是 3rd 方应用程序,中间件通常是执行此操作的理想场所。您的中间件可能类似于:

      def process_response(self, request, response):
          response['MyHttpHeader'] = 'MyHeaderValue'
          return response
      

      【讨论】:

        猜你喜欢
        • 2011-04-10
        • 2011-04-09
        • 1970-01-01
        • 1970-01-01
        • 2010-11-03
        • 2016-04-26
        • 2019-06-28
        • 2012-09-27
        • 2012-06-19
        相关资源
        最近更新 更多