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 -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1e256b81-9e79-88ec-46d9-ee8041e5d712%40oddbird.net. For more options, visit https://groups.google.com/d/optout.
signature.asc
Description: OpenPGP digital signature