Django:用于从经典视图中捕获响应上下文的装饰器

Django: decorator for catching response context from classic views

我有简单的经典 Django 视图:

@log_me
def single(request):
    item = Items.objects.all()[0]
    return render_to_response('ololo.html', {'object': item})

如何在 @log_me 装饰器中捕获上下文?我需要这个:{'object': item}

谢谢。

这是不可能的。 render_to_response 将使用上下文和 return 完成的 HttpResponse 对象呈现模板。 HttpResponse 对象将在您的视图装饰器中可用,但上下文数据已呈现到响应中。

您需要考虑另一种方法。自定义中间件可能是一个选项,请查看 process_template_response.