我希望在实际处理之前先处理一些视图。我了解到基于类的中间件的process_view
方法在这里可能会有所帮助。但问题是我设法从这里得到的唯一的东西是实际的类,但不是它的实例,我想拥有它。
代码方法如下:
class SomeView(View):
class_attribute = "..."
def __init__(self, *args, **kwargs):
self.instance_attribute = "..."
# ...
class Middleware:
# ...
def process_view(request, view_func, view_args, view_kwargs):
# access class_attribute
print(view_func.view_class.class_attribute)
# access instance_attribute
print("I don't know, please help, thank you.")