Django learning notes (7): HttpRequest object and HttpResponse object

Source: Internet
Author: User

1. request and response

These two are good buddies, always inseparable. They can be represented as follows:

Ii. HttpRequest object

A separate HTTP request from a client. The HttpRequest object is automatically created by Django.

It has many attributes. You can refer to DjangoBook. The following are commonly used:

1. method: Request method, for example:

   1:  if request.method == "POST":
   2:      ......
   3:  elif request.mehtod =="GET":
   4:      ......

2. GET and POST dictionary objects

3. COOKIES in dictionary form

4. user:

A django. contrib. auth. models. User object indicates the current Login user. If the current User has not logged on, the user is set to an instance of django. contrib. auth. models. AnonymousUser.

They can be separated from is_authenticated:

   1:  if request.user.is_authenticated():
   2:     ....
   3:  else:
   4:     ....

5. session and dictionary form

6. request. META

It is a Python dictionary that contains all the Header information of this HTTP request, such as the user IP address and user Agent (usually the browser name and version number ). Note that the complete list of Header information depends on the Header information sent by the user and the Header information set on the server. Several common key values in this dictionary are:

HTTP _REFERRER:Link to the webpage before entering the site, if any

HTTP_USER_AGENT, The user-agent string of the user browser, 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_ADDRClient IP address, for example:"12.345.67.89". (If the application is a proxy server, it may contain multiple IP addresses separated by commas, for example:"12.345.67.89, 23.456.78.90".)

   1:  def request_test(request):
   2:      context={}
   3:      try:
   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))
   8:      except Exception,e:
   9:          return HttpResponse("Error:%s" %e)

Note: The GET and POST attributes are all examples of django. http. QueryDict. You can learn more in DjangoBook.

3. Response object

This object is self-created and needs to be instantiated in each view to process and return an HttpResponse object.

1. Structure

The HttpResponse class exists in django. http. HttpResponse and is passed to the page as a string. The following constructor:

2. Set Hearders

You can add and delete headers like dictionaries.

3. HttpResponse subclass

References

Handle error pages such as 404 and 500.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.