The previous Sections introduced the process analysis of the Django request processing, and we learned that Django is around request and Response, that is, no matter "beg" and "should".
When requesting a page, Django wraps the requested metadata data into a HttpRequest object, and Django loads the appropriate view method, passing the HttpRequest object as the first argument to the view method. Any view method should return a HttpResponse object. HttpRequest
The HttpRequest object represents a separate HTTP request from a client. The HttpRequest object is automatically created by Django.
It has a lot of properties, can refer to Djangobook, more commonly used are the following:
1. Method request methods, such as:
1 |
if Request.method = = "POST": |
3 |
elif Request.mehtod = = "GET": |
2. Class Dictionary object get, POST
3. COOKIES, Dictionary Forms
4. User:
A Django.contrib.auth.models.User object represents the currently logged on user, and if the current user is not logged in, user is set to an instance of Django.contrib.auth.models.AnonymousUser.
They can be separated from the is_authenticated () zone:
1 |
If request.user.is_authenticated (): |
5. Session, Dictionary form
6. Request. META
Refer to the request for details. What data is included in the meta. 》。
Request. META is a Python dictionary that contains Header information for all HTTP requests, such as the user's IP address and user Agent (usually the name and version number of the browser). Note that the complete list of header information depends on the header information sent by the user and the header information for the server-side settings. Several common key values in this dictionary are: http_referrer: Pre-pit Link page, if any, http_user_agent, user browser user-agent string, if any. For example: "mozilla/5.0 (X11; U Linux i686; FR-FR; rv:1.8.1.17) gecko/20080829 firefox/2.0.0.17 ". REMOTE_ADDR client IP, such as "12.345.67.89". (If the request is a proxy server, it may be a comma-separated number of IP addresses, such as: "12.345.67.89,23.456.78.90".) ) ......
1 |
def request_test (Request): |
4 |
Http_referer=request. meta[' Http_referrer '] |
5 |
Http_user_agent=request. meta[' Http_user_agent '] |
6 |
Remote_addr=request. meta[' REMOTE_ADDR '] |
7 |
return HttpResponse (' [http_user_agent]:%s,[remote_addr]=%s '% (HTTP_USER_AGENT,REMOTE_ADDR)) |
9 |
Return HttpResponse ("error:%s"%e) |
Note: The GET and post properties are examples of django.http.QueryDict, which can be understood in Djangobook. HttpResponse
The Request and Response objects serve as information transfer between the server and the client. The Request object is used to receive data submitted by the client browser, while the function of the Response object is to send server-side data to the client browser.
For example, in the view layer, it is generally the following code to end a def:
1 |
return HttpResponse (HTML) |
2 |
Return Render_to_response (' nowamagic.html ', {' Data ': Data}) |
For HttpRequest objects, it is created automatically by Django, but the HttpResponse object must be created by ourselves. Each View method must return a HttpResponse object. The HttpResponse class is in Django.http.HttpResponse.
1. Construction HttpRequest
The HttpResponse class exists in Django.http.HttpResponse and is passed to the page as a string. In general, you can construct a HttpResponse object by passing the page contents of a string representation to the HttpResponse constructor:
1 |
>>> response = HttpResponse ("Welcome to Nowamagic.net.") |
2 |
>>> response = HttpResponse ("Text only, please.", mimetype= "Text/plain") |
But if you want to add content incrementally, you can use response as a Filelike object:
1 |
>>> response = HttpResponse () |
2 |
>>> Response.Write ("<p>welcome to Nowamagic.net.</p>") |
3 |
>>> Response.Write ("<p>here ' s another paragraph.</p>") |
You can also pass a iterator as a parameter to HttpResponse without passing a hard-coded string. If you use this technique, here are a few things to keep in mind: iterator should return a string. If HttpResponse is initialized with iterator, the HttpResponse instance cannot be used as a Filelike object. Doing so will throw an exception.
Finally, again, HttpResponse implements the Write () method, which allows you to use the HttpResponse object wherever you want to filelike the object.
2. Set Headers
You can use the dictionary syntax to add, delete headers:
1 |
>>> response = HttpResponse () |
2 |
>>> response[' X-django ' = "It's the best." |
3 |
>>> del response[' x-php '] |
4 |
>>> response[' X-django '] |
3. HttpResponse Sub-category
Mainly for some 404, 500 and other error page processing.
Table H-5. HttpResponse Subclasses |
Class |
Description |
The
httpresponseredirect |
Constructor accepts a single parameter: the URL to which the redirect is redirected. Can be a full URL (e.g, ' http://search.yahoo.com/') or a relative URL (e.g., '/search/'). Note: This will return the HTTP status code 302. |
httpresponsepermanentredirect |
is the same as Httpresponseredirect, but returns a permanent redirect (HTTP Status Code 301). The |
httpresponsenotmodified |
constructor does not require parameters. Use this to designate a page hasn ' t been modified since the user's last request. |
httpresponsebadrequest |
returns the status code. |
httpresponsenotfound |
returns 404 status code. |
Httpresponseforbidden |
returns 403 status code. |
httpresponsenotallowed |
returns 405 status code. It requires a required parameter: A list of allowed methods (e.g., [' GET ', ' POST ']). /td> |
httpresponsegone |
returns 410 status code. |
httpresponseservererror |
returns the status code. |
Of course, you can also define your own HttpResponse subclasses that are not included in the table above.
@csrf_exempt def test_ajax (Request): if Request.method = = "GET": value = Request. Get.get ("name", "Ya") return httpresponseredirect ("/wechat/test/testa?name= ' yourname '"