Python - Django - request objects

request.method:

The method of obtaining the request, such as GET, POST, etc.

views.py:

from django.shortcuts import render, HttpResponse

# request 对象
def test(request):
    print(request.method)
    return render(request, "test.html")

Access page

You can view the request by way of request.method

 

request.GET:

GET method used to obtain the parameters of the URL inside

views.py:

Import the render django.shortcuts from, the HttpResponse 

# Object Request 
DEF Test (Request): 
    Print ( `` request.GET``) # returns a dictionary type 
    print (request.GET.get ( "id") ) # key acquired by the corresponding value 
    return the render (Request, "test.html")

Visit: http: //127.0.0.1:? 8000 / test / id = 2 & username = admin & password = 123456

 

request.POST:

Used to obtain the data submitted over POST

test.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试页面</title>
</head>
<body>

<p>测试页面</p>

<form action="/test/" method="post">
    <input type="text" name="username" value="">
    <input type="submit" name="提交">
</form>

</body>
</html>

views.py:

Import the render django.shortcuts from, the HttpResponse 

# Object Request 
DEF Test (Request): 
    Print (of request.POST) # returns a dictionary type 
    print (request.POST.get ( "username") ) # key acquired by the corresponding value 
    return the render (Request, "test.html")

visit website:

submit

 

request.body:

Request body, byte type, request.POST data is extracted from the body in the

views.py:

from django.shortcuts import render, HttpResponse

# request 对象
def test(request):
    print(request.body)
    return render(request, "test.html")

visit website:

submit:

This two strings are the "Submit" URL encoding

 

request.path_info:

Obtain a user request path does not contain the domain name and URL parameters

from django.shortcuts import render, HttpResponse

# request 对象
def test(request):
    print(request.path_info)
    return render(request, "test.html")

访问:http://127.0.0.1:8000/test/?id=2&username=admin

 

Guess you like

Origin www.cnblogs.com/sch01ar/p/11271914.html