Django 中间件 - 如何使用函数类型中间件的 process_view() 挂钩?
Django middleware - How to use process_view() hook with function type middleware?
我正在使用标准实现来实现一个新的中间件:
def my_middleware(get_response):
def middleware(request):
return get_response(request)
return middleware
我想要view_args
。我可以更改为基于 class 的中间件并实现方法
process_view(request, view_func, view_args, view_kwargs)
有没有其他方法可以在我的中间件中获取这些 view_args、view_kwargs 而无需更改为基于 class 的中间件?
将process_view(...)
设置为middleware(...)
的属性
<b>def process_view(request, view_func, view_args, view_kwargs):
"""
do something
"""</b>
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
<b>middleware.process_view = process_view</b>
return middleware
我正在使用标准实现来实现一个新的中间件:
def my_middleware(get_response):
def middleware(request):
return get_response(request)
return middleware
我想要view_args
。我可以更改为基于 class 的中间件并实现方法
process_view(request, view_func, view_args, view_kwargs)
有没有其他方法可以在我的中间件中获取这些 view_args、view_kwargs 而无需更改为基于 class 的中间件?
将process_view(...)
设置为middleware(...)
的属性
<b>def process_view(request, view_func, view_args, view_kwargs):
"""
do something
"""</b>
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
<b>middleware.process_view = process_view</b>
return middleware