I have a file named utilities.py
in the app
directory, which contains small utility functions specific to my app. I will be calling these function from views.py
. The problem is some utility function requires access to request
object.
Is there any way to use request
object outside the view without passing it as argument to the utility function ?
答案 0 :(得分:0)
我制作了自定义/lib/foo/boar.js
类,该类在Django中间件中初始化,并将链接放置到Context
对象上。
关于以下两个重要事项:
request
类必须继承Context
,以防止在多个线程中运行应用程序时请求泄漏。threading.local
设为单例。我为此使用了Singleton元类。因此,您只需在代码的任何部分进行此调用就可以获取Django请求对象:
Context