Gzip decorator

Rather than using the full GZipMiddleware, you may want to just compress some views. This decorator lets you do that.

@gzip_compress
def your_view(request, ...):
....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

try:fromfunctoolsimportwrapsexceptImportError:fromdjango.utils.functionalimportwraps# Python 2.3, 2.4 fallback.fromdjango.middleware.gzipimportGZipMiddlewaregzip_middleware=GZipMiddleware()defgzip_compress(func):""" Gzip compress an individual view rather than requiring the whole site to use the Gzip middleware. """@wraps(func)defdec(request,*args,**kwargs):response=func(request,*args,**kwargs)returngzip_middleware.process_response(request,response)returndec

More like this

Comments

<p>Note that you can also use django.utils.decorator_from_middleware to achieve the same thing.</p>
<pre>from django.utils import decorator_from_middleware
from django.middleware.gzip import GZipMiddleware
@decorator_from_middleware(GZipMiddleware)
def my_view(request):
pass
</pre>