1. Get the parameters in the URL path
Requirement: assume that the user accesses 127.0.0.1/user/1/2. What should I do?
(1) unnamed parameters (location parameters)
# URLs in the project. add settings under py: URL (R' ^ user/(\ D +) $ ', views. index) # in user. in the index view of views: def index (request, a, B): # Return httpresponse ("Get Data % S % s" % (, B ))
(2) name parameters (keyword parameters)
# Add the URL (R' ^ user /(? P <Category> \ D + )/(? P <ID> \ D +) $ ', views. index) # in user. in the index view of views: def index (request, ID, category): # accepted parameters do not need to return httpresponse ("Get Data % S % s" % (category, ID ))
The output result is 1 2.
2. Obtain the query string
Requirement: Get 127.0.0.1: 8000/user? Id = 1 & pid = 99 query string value
# URLs in the project. add settings under py: URL (R' ^ user/$ ', views. index) # in user. in the index view of views, Def index (request): Id = request. get. get ("ID") pid = request. get. get ("PID") return httpresponse ("Get Data % S % s" % (ID, pid ))
Note: obtaining a query string has nothing to do with the request method: whether it isGETOrPOSTAll requests can be passed throughrequest.GETProperties to obtain !!!
3. Get form data
Use postman to send a form request.
Def index (request): Id = request. post. get ("ID") pid = request. post. get ("PID") return httpresponse ("Get Data % S % s" % (ID, pid ))
Note:request.POSTIt can only be used to obtain the Request body form data in post mode!
4. Obtain non-form types
request.bodyAttribute: Get non-Form Type Request body data, such as JSON and XML. The retrieved data type isbytesType
- After obtaining the data, parse the data and retrieve the parameters.
Def index (request): json_str = request. Body json_str = json_str.decode ()# Python3.6 and above do not use this codeDict_data = JSON. Loads (json_str)# Loads converts STR to dict and dumps converts dict to StrId = dict_data.get ("ID") pid = dict_data.get ("PID") return httpresponse ("Get Data % S % s" % (ID, pid ))
5. Get the content of the Request Header
Common request headers are as follows:
CONTENT_LENGTH-The length of the Request body (as a string ).
CONTENT_TYPE-The MIME type of the Request body.
HTTP_ACCEPT-Acceptable content types for the response.
HTTP_ACCEPT_ENCODING-Acceptable encodings for the response.
HTTP_ACCEPT_LANGUAGE-Acceptable versions for the response.
HTTP_HOST-The HTTP Host header sent by the client.
HTTP_REFERER-The referring page, if any.
HTTP_USER_AGENT-The client's User-Agent string.
QUERY_STRING-The query string, as a single (unparsed) string.
REMOTE_ADDR-The IP address of the client.
REMOTE_HOST-The hostname of the client.
REMOTE_USER-The User Authenticated by the Web server, if any.
REQUEST_METHOD-A string such"GET"Or"POST".
SERVER_NAME-The hostname of the server.
SERVER_PORT-The port of the server (as a string ).
Get the metadata of the Request Header
Example:
Def index (request): IP = request. Meta. Get ("remote_addr") return httpresponse ("your IP address is % s" % IP)6. Get the content of the custom request header
Use postman to add a custom request header, key = ID, value = 1. So how should we get it?
The Code is as follows:
Def index (request): Id = request. Meta. Get ("http_id") return httpresponse ("your ID: % s" % ID)
Note: you must add a prefix when getting the custom request header attribute value.HTTP_And converts it to uppercase to obtain the value as a key.
Appendix (methods for obtaining Request Parameters ):
| Attribute |
Description |
| Path |
The full path of the Request page, excluding the domain name port parameters. For example:/users/index |
| Method |
A fully capitalized string that indicates the HTTP Method Used in the request. common values:GET,POST,DELETE,PUT. The following three types are:GETRequest:
Form form is submitted by default (or the method is specified as get)
Enter the address in the browser for Direct Request
Hyperlink (a tag) in the webpage)
|
| User |
- Logged On: abstractuser object;
- Not logged on: anonymoususer object;
Determine if you have logged on:request.user.is_authenticated(), Returns true, indicating that you have logged on
|
| Get |
Dictionary-likeQueryDictObject, including all query string parameters in the URL |
| Post |
Dictionary-likeQueryDictObject, includingPOSTAll key-Value Pair parameters requested (form data) |
| Body |
Obtain the original request body data. The obtained data isBytes type |
| Files |
A dictionary-like object that contains all uploaded files |
| Meta |
pythonDictionary type, which encapsulates the data in the Request Header headers. -REMOTE_ADDR-Client IP Address -REQUEST_METHOD-A string, such as "get" or "Post -CONTENT_TYPE-MIME type of the Request body Note:For the key values added to the Request Header, Django adds the key prefixHTTP_Convert to uppercase and save the key value to request. Meta. |
| Cookies |
A standardpythonDictionary, including allcookies, Keys and values are both strings |
| Session |
Readable and writable dictionary-like objects:django.contrib.sessions.backends.db.SessionStore.
DjangoProvidedsessionModule, which is enabled by default for savingsessionData |
Django get Request Parameters