Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Introduction – You’ve written a lot of view functions with this blog editor, but each view function takes a parameter called request. Curious what object the request is received by the view function!!
The HttpRequest object
1. We can print the request object. Let’s see:
<WSGIRequest: GET '/music/test3/'>
Copy the code
We know that WSGIRequest is an HTTP request object that contains the submission method and URL path.
To sum up:
After receiving the HTTP request, the server will create an HttpRequest object (it is a request object: user information (submission method, etc.); Browser information (request headers, etc.).
The first argument to the view function is an HttpRequest object. An API for HttpRequest objects is defined in the Django. HTTP module
2. We can print and see what methods it has:
dir(request)
Copy the code
Common attributes and methods:Properties:
- Path: A string representing the full path to the requested page, excluding the domain name.
- Method: A string representing the HTPP method used in the request. Common values include ‘GET’ and ‘POST’.
- Encoding: A string that specifies the encoding method of the submitted data. If None, the browser’s default Settings (typically UTF-8) are used. This property is writable and can be modified to modify the encoding used to access the form data, plus any access to the property will use the new Encoding value.
- GET: a dictionary-like object that contains all the parameters of the GET request.
- POST: A dictionary-like object that contains all the parameters of the POST request.
- FILES: a dictionary-like object that contains all uploaded FILES.
- COOKIES: A standard Python dictionary containing all COOKIES with strings of keys and values.
- Session: A dictionary-like object that is both readable and writable and represents the current session, available only if Django has session support enabled.
Methods:
- Is_ajax (): Returns True if the request was initiated via XMDHttpRequest.
🔆 In The End!
Start now, stick to it, a little progress a day, in the near future, you will thank you for your efforts! |
---|
This blogger will continue to update the basic column of crawler and crawler combat column, carefully read this article friends, you can like the collection and comment on your feelings after reading. And can follow this blogger, read more crawler in the days ahead!
If there are mistakes or inappropriate words can be pointed out in the comment area, thank you! If reprint this article please contact me for my consent, and mark the source and the name of the blogger, thank you!