path('<int:id>/', views.client),
我有一个中间件,我需要从网址获取。我试图放入__call__(self, request, id)
,但它不起作用。任何人都知道如何实现这一目标
class SimpleMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request, id): <---error
from django.http import HttpResponse
return HttpResponse('before middleware' + id) <----error
response = self.get_response(request)
from django.http import HttpResponse
return HttpResponse('after middleware')
return response
您可以在中间件类的process_view
函数中获取 URL 解析器信息。
class SimpleMiddleware:
def process_view(request, view_func, view_args, view_kwargs):
pass