Django Views
View layer
A view function, or view for short, is a simple Python function that accepts a web request and returns a web response.
A response can be an HTML page, a 404 error page, a redirect page, an XML document, or an image...
Regardless of what logic the view itself contains, it must return a response. The code can be written anywhere, as long as it is under the Python directory; generally it is placed in the project's views.py file.
Each view function is responsible for returning an HttpResponse object, which contains the generated response.
There are two important objects in the view layer: the request object (request) and the response object (HttpResponse).
Request object: HttpRequest object (referred to as the request object)
The following introduces several commonly used request attributes.
1、GET
The data type is QueryDict, a dictionary-like object that contains all the parameters of HTTP GET.
If there are duplicate keys, all the values are put into the corresponding list.
Value retrieval format: object.method.
get(): returns a string; if a key has multiple values, the last value of that key is retrieved.
Example
def
runoob
(
request
)
:
name
=
request.
GET
.
get
(
"name"
)
return
HttpResponse
(
'Name: {}'
.
format
(
name
)
)

2、POST
The data type is QueryDict, a dictionary-like object that contains all the parameters of HTTP POST.
Commonly used in a form; the name attribute of a tag in the form corresponds to the parameter key, and the value attribute corresponds to the parameter value.
Value retrieval format: object.method.
get(): returns a string; if a key has multiple values, the last value of that key is retrieved.
Example
def
runoob
(
request
)
:
name
=
request.
POST
.
get
(
"name"
)
return
HttpResponse
(
'Name: {}'
.
format
(
name
)
)

3、body
The data type is a binary byte stream, the parameter content in the raw request body; in HTTP it is used for POST, because GET has no request body.
Not commonly used in HTTP, but very useful when handling non-HTTP form messages, such as: binary images, XML, Json, etc.
Example
def
runoob
(
request
)
:
name
=
request.
body
(
name
)
return
HttpResponse
(
"Runoob Tutorial"
)

4、path
Get the path part of the URL; the data type is a string.
Example
def runoob(request):
name = request.path
print(name)
return HttpResponse("Runoob Tutorial")

5、method
Get the method of the current request; the data type is a string, and the result is uppercase.
Example
def
runoob
(
request
)
:
name
=
request.
method
(
name
)
return
HttpResponse
(
"Runoob Tutorial"
)

Response object: HttpResponse object
Response objects mainly have three forms: HttpResponse(), render(), redirect().
HttpResponse(): Returns text; the parameter is a string, in which the text content is written. If the string parameter contains html tags, they can also be rendered.
Example
def
runoob
(
request
)
:
return HttpResponse("Runoob Tutorial")
return
HttpResponse
(
)

render(): Returns text; the first parameter is request, the second is a string (page name), and the third is a dictionary (optional parameter, parameters passed to the page: the key is the page parameter name, the value is the views parameter name).
Example
def
runoob
(
request
)
:
name
=
"Runoob Tutorial"
return
render
(
request
,
"runoob.html"
,
{
"name"
:name
}
)

redirect(): redirect, jump to a new page. The parameter is a string, in which the page path is filled. Generally used after a form is submitted to jump to a new page.
Example
def
runoob
(
request
)
:
return
redirect
(
"/index/"
)
render and redirect are wrapped on the basis of HttpResponse:
- render: the underlying return is also an HttpResponse object
- redirect: the underlying inheritance is the HttpResponse object