Using django-piston, how can I write HTTP headers in a response?

How to include an HTTP header, such as Cache-Control or Last-Modified, in response to a django-piston call?

+3
source share
2 answers

You can wrap it in your own urls.pyaccording to the procedure in specifying the presentation cache in the urlconf manual in the Django documentation. In my case, I had my Piston API in a separate module and I prefer to use Varnish instead of the built-in Django caching structure, so I used this approach in mine api/urls.py(which belongs to my main one urls.py) to set cache headers I wanted :

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

-, :

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

, , . Middleware , . :

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

Source: https://habr.com/ru/post/1739030/


All Articles