Задать вопрос

Тел: +7 965 3737 888

527

Просмотров

0

Ответов

Other approach of making middleware (by decorators)

<p>Other approach of making middleware. Advantage of is to specify, which middleware is used for which view function and in what order. Middleware function gets all arguments, that are passed to view function.
</p>
<p><strong>Example usage</strong>
</p>
@RequestMiddleware
def print_params_middleware(request, *args, **kwargs):
    print 'GET params:', request.GET

@ResponseMiddleware
def modify_headers_middleware(request, response, *args, **kwargs):
    response['Content-Type'] = 'text/html'

@ExceptionMiddleware
def catch_error_middleware(request, e, *args, **kwargs):
    return HttpResponse('catched error %s' % e )

@modify_headers_middleware
@catch_error_middleware
@print_params_middleware
def some_view(request, *args, **kwargs):
    print 'someview'
    return HttpResponse()

Вопрос полезен? Да0/Нет0
file_4512.py(1.6Кб)
None