包装非通用 Django 视图

发布于 2024-08-24 11:52:49 字数 406 浏览 10 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛徒 2024-08-31 11:52:49

我认为上下文处理器是可行的方法,但是其中有一些逻辑可以检查请求路径(例如),并且只需在需要时挖掘并设置变量即可

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文