开发者

Best way to request-scope data in Django?

开发者 https://www.devze.com 2023-01-31 13:51 出处:网络
I\'m wondering if there\'s a clever pattern for request-scoping arbitrary information without resorting to either TLS or putting the information in the session.

I'm wondering if there's a clever pattern for request-scoping arbitrary information without resorting to either TLS or putting the information in the session.

Really, this would be for contextual attributes that I'd like to not look up more than once in a request path, but which are tied to a request invocation and there's no good reason to let them thresh around in the session.

Something like a dict that's pinned to the request where I can shove things or lazy load t开发者_如何学Chem. I could write a wrapper for request and swap it out in a middleware, but I figured I'd check to see what best-practice might be here?


Just assign the dictionary directly to the request. You can do that in middleware or in your view, as you like.


Context processors. They are called once for every request and receive the actual request object - so you can add ANY data to the context, also based on the curent request!

0

精彩评论

暂无评论...
验证码 换一张
取 消