如何在 Django 中创建请求对象?
因此,我将 Django 与 Google App Engine 结合使用,并且有一个 urls.py 文件,它将每个 url 重定向到相应的方法。这些方法中的每一个都会自动传递“request”作为参数之一,我相信它是一个 HttpRequest 对象。
如何从我的代码中创建这个填充的请求对象?例如,如果我在代码深处的某个方法中,我希望能够访问此请求对象,而不必将其传递给每个函数以确保它可用。假设 urls.py 调用方法 foo,我当前执行的方式是:
foo(request):
# stuff here
bar(request)
# more stuff here
bar(request):
# stuff here<stuff>
baz(request)
# more stuff here
baz(request):
do something with request here
这似乎是错误的,因为我必须通过不需要它的函数传递请求,以便我可以在 baz 中使用它。
我想做一些类似的事情:
foo(request):
# stuff here
bar()
# more stuff here
bar():
# stuff here
baz()
# more stuff here
baz():
request = HttpRequest()
do something with request here
即如果不需要,就不要传递请求。但是,执行 request = HttpRequest() 返回一个空的请求对象...我想要的是一个完全填充的版本,就像传递到从 urls.py 调用的每个方法中的内容一样。
我在这里浏览了 HttpRequest 的文档: http://docs.djangoproject.com/en/dev/ref/request-响应/ 但没有看到如何做到这一点。
任何想法将不胜感激。
谢谢, 瑞安
So I'm using Django with Google App Engine and I have an urls.py file that redirects each url to a corresponding method. Each one of those methods is automatically passed "request" as one of the arguments, which I believe is an HttpRequest object.
How do I create this populated request object from within my code? For example, if I'm within some method deep within my code, I'd like to have access to this request object without having to pass it to every function to make sure it's available. Assuming urls.py calls the method foo, the way I'm currently doing it is:
foo(request):
# stuff here
bar(request)
# more stuff here
bar(request):
# stuff here<stuff>
baz(request)
# more stuff here
baz(request):
do something with request here
This seems wrong because I'm having to pass request through functions that don't need it just so that I have it available in baz.
I'd like to do something like:
foo(request):
# stuff here
bar()
# more stuff here
bar():
# stuff here
baz()
# more stuff here
baz():
request = HttpRequest()
do something with request here
i.e. not pass request around if I don't have to. However, doing request = HttpRequest()
returns an empty request object...what I want is a fully populated version, like what is passed into each method called from urls.py.
I glanced through the documentation for HttpRequest here:
http://docs.djangoproject.com/en/dev/ref/request-response/
but didn't see the way to do it.
Any thoughts would be greatly appreciated.
Thanks,
Ryan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
request = HttpRequest()
会给你一个空的,但你可以在里面写一些东西。这是我在项目中使用的一个示例:
request = HttpRequest()
will give you a empty one, but you can write something to it.Here is an example I used in my project :
您可以从测试包中获得几乎真实的包,而不是创建自己的包。像这样的东西:
Instead of creating your own, you can get an almost real one from test package. Something like that:
只需在视图定义之前创建一个请求变量,并在从视图接收它时设置其值(使用示例代码):
请参阅:Python 变量作用域注释
更新: 这个问题与您的问题非常相似,接受的解决方案基本上是创建一个全局请求变量并将其附加到设置中。
Just create a request variable before the view definitions and set its value when you receive it from the view (using your example code):
See: Notes on Python variable scope
Update: This question is very similar to yours and the accepted solution is basically creating a global request variable and attaching it to the settings.