包装非通用 Django 视图
我想在 的上下文中注入一个附加变量查看属于第 3 方应用程序的,无需编辑该应用程序的代码。有没有办法通过包装(非通用)视图来做到这一点?它不接受 extra_context 参数,因此此SO线程 不起作用。我知道我可以创建一个上下文处理器,但这似乎是很大的开销,当我只想在一个视图中使用这个变量时,在每个页面上都可用该变量。或者有没有办法缩小上下文处理器的范围?
I want to inject one additional variable into the context of a view that belongs to a 3rd-party application without editing the code of that application. Is there a way to do this by wrapping the (non-generic) view? It doesn't accept an extra_context parameter, so the approach described in this SO thread won't work. I know I could create a context processor, but that seems like a lot of overhead, having this variable available on every page when I just want it in one view. Or is there a way to narrow the scope of a context processor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为上下文处理器是可行的方法,但是其中有一些逻辑可以检查请求路径(例如),并且只需在需要时挖掘并设置变量即可
I think a context processor is the way to go, but with a splash of logic in there that checks the request path (for instance) and only bothers digging out and setting the variable for the occasions when it's needed