如何在 Django 中创建请求对象?

发布于 2024-08-18 02:30:54 字数 1104 浏览 4 评论 0原文

因此,我将 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 技术交流群。

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

发布评论

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

评论(3

踏雪无痕 2024-08-25 02:30:54

request = HttpRequest() 会给你一个空的,但你可以在里面写一些东西。

这是我在项目中使用的一个示例:

def new(request):
    ...
    newrequest = HttpRequest()
    newrequest.method = 'GET'
    newrequest.user = request.user
    resp = result_email(newrequest , obj-id , token )
    send_email( resp , ... )
    return HttpResponseRedirect( ... )
    ...
def result_email(request , ...):
    ...
    return render(request , ...)

request = HttpRequest() will give you a empty one, but you can write something to it.

Here is an example I used in my project :

def new(request):
    ...
    newrequest = HttpRequest()
    newrequest.method = 'GET'
    newrequest.user = request.user
    resp = result_email(newrequest , obj-id , token )
    send_email( resp , ... )
    return HttpResponseRedirect( ... )
    ...
def result_email(request , ...):
    ...
    return render(request , ...)
神经大条 2024-08-25 02:30:54

您可以从测试包中获得几乎真实的包,而不是创建自己的包。像这样的东西:

from django.test import Client

client = Client()

# So here you can imitate needed you request with specific method
request = client.get('/').wsgi_request # or client.get('/').request

print(request.user)
>>> AnonymousUser

Instead of creating your own, you can get an almost real one from test package. Something like that:

from django.test import Client

client = Client()

# So here you can imitate needed you request with specific method
request = client.get('/').wsgi_request # or client.get('/').request

print(request.user)
>>> AnonymousUser
锦爱 2024-08-25 02:30:54

只需在视图定义之前创建一个请求变量,并在从视图接收它时设置其值(使用示例代码):

current_request = None

foo(request):
    current_request = request
    # stuff here
    bar()
    # more stuff here

bar():
     # stuff here
    baz()
     # more stuff here

baz():
    # do something with currest_request here

请参阅: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):

current_request = None

foo(request):
    current_request = request
    # stuff here
    bar()
    # more stuff here

bar():
     # stuff here
    baz()
     # more stuff here

baz():
    # do something with currest_request here

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.

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