Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么在django.utils.deprection.py中声明中间件mixin_Python_Django_Middleware_Django 1.10 - Fatal编程技术网

Python 为什么在django.utils.deprection.py中声明中间件mixin

Python 为什么在django.utils.deprection.py中声明中间件mixin,python,django,middleware,django-1.10,Python,Django,Middleware,Django 1.10,在pathdjango.utils.deprecation.py中,我们有一些关于方法的弃用警告的类 在该文件中,我们有一个名为MiddlewareMixin的类。此类用于编写中间件类。尽管与弃用无关,但为什么此类使用此路径编写?简言之:它是一种将弃用的中间件转换为新中间件的工具,尽管它有一些局限性 Django的中间件“风格”已经改变。此MiddlewareMixin在大多数情况下可以将旧样式的中间件类“转换”为新样式的中间件装饰器,如中所述: 类django.utils.deprecatio

在path
django.utils.deprecation.py中,我们有一些关于方法的弃用警告的类


在该文件中,我们有一个名为
MiddlewareMixin
的类。此类用于编写中间件类。尽管与弃用无关,但为什么此类使用此路径编写?

简言之:它是一种将弃用的中间件转换为新中间件的工具,尽管它有一些局限性

Django的中间件“风格”已经改变。此
MiddlewareMixin
在大多数情况下可以将旧样式的中间件类“转换”为新样式的中间件装饰器,如中所述:

类django.utils.deprecation.MiddlewareMixin

(……)

在大多数情况下,继承此mixin将足以使旧式中间件与新系统兼容,并具有足够的向后兼容性。新的短路语义将对现有中间件无害甚至有益。在少数情况下,中间件类可能需要一些更改以适应新的语义

在“旧时代”(之前),中间件的编写方式如下:

class SomeMiddleware:

    def process_request(self, request):
        # ...
        pass

    def process_response(self, request, response):
        # ...
        return response
但如今,中间件更多地被视为围绕“底层中间件”的某种“装饰器”,并最终成为视图。如合同规定:

中间件可以写成如下所示的函数:
def simple_middleware(get_response):
    # One-time configuration and initialization.

    def middleware(request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.

        response = get_response(request)

        # Code to be executed for each request/response after
        # the view is called.

        return response

    return middleware
通过引入一种新的“样式”,您可以看到旧的中间件本身已经“弃用”,这当然是一个遗憾,因为以前编写的所有中间件现在都将被视为无效

然而,
MiddlewareMixin
能够在现代中间件中转换这样的旧中间件,它通过覆盖
\uuuuuuu调用
函数来实现这一点,并因此在两者之间调用
处理请求
处理响应
,如图所示:

因此,在这里,我们通过覆盖
\uuu call\uu
函数使对象可调用,从而模仿新样式中的
def中间件(请求)
的工作方式。但是,如果在旧的中间件中,
\uuu调用\uu
也被覆盖,那么这当然会导致一些问题。此外,旧式中间件具有一些功能,如
过程视图
过程异常
,以及
过程模板响应
,这些功能在这里不再使用。但我的想法是,无论如何,这些都不是很“流行”。

我认为这是因为它将“旧”样式的中间件转换为“新”样式的中间件。“旧”中间件具有
process\u request
process\u response
,但“新”中间件实质上是“下面一层”上的装饰器。此mixin将旧样式(已弃用)转换为新样式。
class MiddlewareMixin:
    def __init__(self, get_response=None):
        self.get_response = get_response
        super().__init__()

    def __call__(self, request):
        response = None
        if hasattr(self, 'process_request'):
            response = self.process_request(request)
        response = response or self.get_response(request)
        if hasattr(self, 'process_response'):
            response = self.process_response(request, response)
        return response