正如标题所述,我想在这里使用'decorator_from_middleware'函数:https://docs.djangoproject.com/en/2.1/_modules/django/utils/decorators/
然而,我只是对如何正确使用它感到困惑。我有我的自定义中间件类和所有正常的中间件设置。在装饰师的帮助下,我将如何使用这个函数来使用我的中间件作为每个视图的基础
例如: 假设我有一些中间件类
class SimpleMiddleware:
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
response = self.get_response(request)
# Code to be executed for each request/response after
# the view is called.
return response
如何使用decorator_from_middleware(middleware_class):
并将其应用于特定视图
注意:您的中间件可能无法工作,因为文档断言它需要与旧式中间件方法兼容Check it out
假设此代码是您的视图模块:
若要将此应用于基于类的视图,则需要修饰分派方法。您甚至可以这样编写一个mixin:
相关问题 更多 >
编程相关推荐