104. HttpRequest objects Detailed

Object WSGIRequest common attributes and methods:

WSGIRequest objects common attributes:

WSGIRequest objects Most of the properties are read-only, because these properties are uploaded from the client up, no need to make any changes. The following are some common attributes explain:

1.path: Request complete "path" of the server, but does not contain the domain name and parameters. For example http://www.baidu.com/xxx/yyy/, then the path is / xxx / yyy /.
2.method: http method represents the current request, such as a GET or POST.
3. GET: a django.http.request.QueryDict object. The operation is similar to the dictionary. This property contains all to? xxx xxx = parameter in upload up. It contains all of the query string.
4.POST: is a django.http.request.QueryDict object. This property contains all the POST method to upload up parameters.
5.FILES: is a django.http.request.QueryDict object. This property contains all the POST method to upload up parameters.
6.COOKIES: A standard Python dictionary containing all of the cookie, key-value pair is a string type.
7.sessions: a dictionary of similar objects. For operating the server session.
8.META: the client sends the stored up all the header information.
1.CONTENT_LENGTH: the length of the body of the request (a string).
2.CONTENT_TYPE: MIME type text request.
3.HTTP_ACCEPT: Content-Type Response receivable.
4.HTTP_ACCEPT_ENCODING: response may be received encoded.
5.HTTP_ACCEPT_LANGUAGE: receiving the response may language.
6.HTTP_HOST: HOST value sent by the client.
7.HTTP_REFERER: visit this page on a page url.
8.QUERY_STRING: a single string in the form of a query string (not parsed form).
9.REMOTE_ADDR: IP address of the client. If the server uses a reverse proxy or nginx to do is load balancing, then the value returned is 127.0.0.1, which can be used when HTTP_X_FORWAROED_FOR to get, so get the code fragment ip address is as follows:
if request.META.has_key('HTTP_X_FORWARDED_FOR'):
    ip = request.META['HTTP_X_FORWARDED_FOR']
else:
    ip = request.META['REMOTE_ADDR']
REMOTE_HOST: host name of the client.
REQUEST_METHOD: request method. Similar to a string GET or POST.
SERVER_NAME: Server domain.
SERVER_PORT: server port number is a string type.

WSGIRequest objects commonly used methods:

1.is_secure (): whether the use of https protocol.
2.is_ajax (): whether the requested using ajax transmitted. Principle is to determine whether there is a request for X-Requested-With header: XMLHttpRequest.
print(request.is_ajax())
# False 通过判断请求的头中是否存在,X-Responsed-With=XMLHttpRequest =>True
3.get_host (): the domain name server. If there is a port number in the time of the visit, it will add the port number. For example: www.baidu.com: 9000.

4.get_full_path (): returns the full path. If you have a query string will be added to the query string, such as / book /? Username = guyan.

5.get_raw_uri (): get the full url request.

QueryDict objects:

We usually use request.GET objects and request.POST are QueryDict object that inherits dict, dict therefore usage with almost the same. Which use more of is get and getlist method.
1.get method: used to obtain the specified key value, if not the key, it will return None.
2.getlist: If the browser to upload the key values ​​corresponding to a plurality of, then you need to get through this method.

== if the client sends the GET request returns the user to add information page; if the client sends a POST request, the client sends the information will be saved to the database. == sample code is as follows:

def add(request):
   == # 如果客户端发送的是get请求,就返回一个添加用户的页面;
    # 如果客户端发送的是POST请求,就将客户端发送的信息进行保存到数据库中==
    if request.method == 'GET':
        return render(request, 'book/static/add.html')
    else:
        title = request.POST.get('title')
        content = request.POST.get('content')
        teconologies = request.POST.getlist('teconologies')
        print(teconologies)
        users = User.objects.create(title=title, content=content, teconologies=teconologies)
        return HttpResponse('add 成功')

== add.html sample code is as follows wherein: ==

<form action="{% url 'add' %}" method="post">
    <table>
        <tbody>
        <tr>
            <td>作者</td>
            <td><input type="text" name="title" ></td>
        </tr>
        <tr>
            <td>内容</td>
            <td><input type="text" name="content"></td>
        </tr>
        <tr>
            <td>擅长技术:</td>
            <td>Python<input type="checkbox" name="teconologies"></td>
            <td>Django<input type="checkbox" name="teconologies"></td>
        </tr>
        <tr><td><input type="submit" value="提交"></td></tr>
        </tbody>
    </table>
</form>

Guess you like

Origin www.cnblogs.com/guyan-2020/p/12293260.html