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

时间:2022-01-30 22:02:56

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.

所以我正在使用Django和Google App Engine,我有一个urls.py文件,可以将每个网址重定向到相应的方法。这些方法中的每一个都自动传递“request”作为参数之一,我相信它是一个HttpRequest对象。

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:

如何在我的代码中创建这个填充的请求对象?例如,如果我在我的代码深处的某个方法中,我希望能够访问此请求对象,而不必将其传递给每个函数以确保它可用。假设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

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.

这似乎是错误的,因为我必须通过不需要它的函数传递请求,以便我可以在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.

即如果我不需要,也不会通过请求。但是,执行request = HttpRequest()会返回一个空的请求对象...我想要的是一个完全填充的版本,就像从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.

我在这里浏览了HttpRequest的文档:http://docs.djangoproject.com/en/dev/ref/request-response/但是没有看到这样做的方法。

Any thoughts would be greatly appreciated.

任何想法将不胜感激。

Thanks, Ryan

2 个解决方案

#1


0  

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

请参阅:有关Python变量范围的说明

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.

更新:此问题与您的问题非常相似,并且已接受的解决方案基本上是创建全局请求变量并将其附加到设置中。

#2


1  

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

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

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 , ...)

#1


0  

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

请参阅:有关Python变量范围的说明

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.

更新:此问题与您的问题非常相似,并且已接受的解决方案基本上是创建全局请求变量并将其附加到设置中。

#2


1  

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

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

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 , ...)