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
The Django framework uses client-server architecture to implement web applications. When a client requests for a resource, a HttpRequest object is created and correspond view function is called that returns HttpResponse object. To handle request and response, Django provides HttpRequest and HttpResponse classes.
To represent database-table data in Python objects, Django uses an intuitive system: A model class represents a database table, and an instance of that class represents a particular record in the database table. To create an object, instantiate it using keyword arguments to the model class, then call save () to save it to the database.
It returns True if the request was made via an XMLHttpRequest. Start the server and get access to the browser. It shows the request method name at the browser. This class is a part of django.http module. It is responsible for generating response corresponds to the request and back to the client.
Once you’ve created your data models, Django automatically gives you a database-abstraction API that lets you create, retrieve, update and delete objects. This document explains how to use this API.
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 , ...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With