Django上下文处理器:是否可以访问ContextProcessor中的当前上下文? [英] Django Context Processors: Is it possible to access current context in ContextProcessor?

查看:171
本文介绍了Django上下文处理器:是否可以访问ContextProcessor中的当前上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法可以访问自定义上下文处理器中的视图传递的当前上下文,所以如果我想要而不是覆盖现有变量,我可以添加缺失的变量?

Is there a way I can access current context passed by view in custom context processor so I can add missing variable if I want rather than overriding existing variable ?

我正在努力实现:

我构建了我的URL这个/ city_slug /我想检查城市变量是否已经存在于上下文中,否则我想添加城市到我的上下文(可能使用存储在会话变量中的最后使用的城市,否则默认为一些城市,甚至可以设置会话变量下一次使用。)

I construct my URL's like this /city_slug/ and I want to check if city variable already exist in context, otherwise I want to add city to my context (may be using the last used city stored in session variable otherwise default to some city and may be even set session variable for next use.)

我认为这是很常见的问题,你们如何解决?

I think this is very common problem, How do you guys solve it ?

推荐答案

您无法从上下文处理器中访问当前上下文,我也猜测中间件不会是正确的解决方案为你。也许这是有意义的,创建您自己的模板渲染功能,您在视图中使用,您总是传递它的上下文和当前的请求,并放置您想要的功能,并使用通常的渲染函数渲染模板django提供给你!

You cannot access the current context from within a context processor, and I guess also a middleware wouldn't be the right solution for you. Maybe it would make sense to create your own template render function that you use in your views and you always pass it the context and the current request and put the functionality you want in it and render the template at the end with the usual rendering functions django provides you!

这篇关于Django上下文处理器:是否可以访问ContextProcessor中的当前上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆