New-style middleware

30 views
Skip to first unread message

Torsten Bronger

unread,
Nov 22, 2016, 4:31:26 PM11/22/16
to django...@googlegroups.com
Hallöchen!

Considering the following old-style middleware class:

class ExceptionsMiddleware:
def process_exception(self, request, exception):
...

I convert this to new-style middleware by inserting two methods:

class ExceptionsMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
return self.get_response(request)
def process_exception(self, request, exception):
...

Is this really correct? Actually, the old way looks more concise to
me. In particular, is there a reason why Django does not provide a
non-deprecated base class for middleware like:

class Middleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
return self.get_response(request)

(I know MiddlewareMixin but it is deprecated.)

Tschö,
Torsten.

--
Torsten Bronger

Carl Meyer

unread,
Nov 22, 2016, 6:06:26 PM11/22/16
to django...@googlegroups.com
Hi Torsten,

I worked on the design and implementation of new-style middleware.

On 11/22/2016 01:30 PM, Torsten Bronger wrote:
> Hallöchen!
>
> Considering the following old-style middleware class:
>
> class ExceptionsMiddleware:
> def process_exception(self, request, exception):
> ...
>
> I convert this to new-style middleware by inserting two methods:
>
> class ExceptionsMiddleware:
> def __init__(self, get_response):
> self.get_response = get_response
> def __call__(self, request):
> return self.get_response(request)
> def process_exception(self, request, exception):
> ...
>
> Is this really correct? Actually, the old way looks more concise to
> me. In particular, is there a reason why Django does not provide a
> non-deprecated base class for middleware like:
>
> class Middleware:
> def __init__(self, get_response):
> self.get_response = get_response
> def __call__(self, request):
> return self.get_response(request)

Yes, I agree with you that for middleware which don't implement request
or response processing (only process_exception, process_view, or
process_template_response), the new style is less concise, because it
requires implementing a boilerplate __init__ and __call__. To be honest,
the primary consideration in the new middleware was request and response
processing, and your case did not receive as much attention as it
perhaps should have.

I've considered a few possible approaches to fix this, and I think your
suggestion of a built-in base class that implements the
simplest-possible version of __init__ and __call__ makes sense; it is
the least magical and most explicit option.

Carl

signature.asc

Torsten Bronger

unread,
Nov 23, 2016, 5:56:38 AM11/23/16
to django...@googlegroups.com
Hallöchen!

Carl Meyer writes:

> [...]
>
> Yes, I agree with you that for middleware which don't implement
> request or response processing (only process_exception,
> process_view, or process_template_response), the new style is less
> concise, because it requires implementing a boilerplate __init__
> and __call__. To be honest, the primary consideration in the new
> middleware was request and response processing, and your case did
> not receive as much attention as it perhaps should have.

Thanks for the explanation! The boilerplate is ineed not too bad.
I just wanted to make sure to not have overlooked something.

Regards,
Torste.

--
Torsten Bronger

Reply all
Reply to author
Forward
0 new messages