How does Django get front end data from request
- 2021-11-02 01:18:33
- OfStack
Django
###request
If urls. py is the bridge between front page and background program in Django, then request is the car responsible for transportation on the bridge
It can be said that almost all the information received by the back end to the front end comes from requests.
Properties of # # request Object
request.META
request. MATE retrieves a standard python dictionary. It contains all the HTTP request information
1 CONTENT_LENGTH--The length of the body of the request (which is a string).
2 CONTENT_TYPE-The MIME type of the body of the request.
3 HTTP_ACCEPT-Content-Type in response to acceptability.
4 HTTP_ACCEPT_ENCODING-Responses to acceptable encoding.
5 HTTP_ACCEPT_LANGUAGE-A language in which responses can be received.
6 HTTP_HOST-HTTP Host header sent by customer service.
7HTTP_REFERER-Referring page.
8 HTTP_USER_AGENT-The user-agent string for the client.
9 QUERY_STRING--Query string in the form of a single string (unparsed form).
10 REMOTE_ADDR-The client's IP address.
11 REMOTE_HOST-The host name of the client.
12 REMOTE_USER-Server authenticated user.
13 REQUEST_METHOD-A string, such as "GET" or "POST".
14 SERVER_NAME-The host name of the server.
15 SE0RVER_PORT-The port of the server (is a string)
Get the source host of the request: HttpRequest.get_host ()
request.scheme
The mode of request, that is, http or https
request.path
The path of the request, where the path refers to the relative path, that is to say, the path of a request to log in to the background page: http://127.0. 0.1: 8000/admin is/admin
Get the full path (including parameters): HttpRequest.get_full_path ()
Get Absolute url: HttpRequest.bulid_absolute_uri (location) This parameter defaults to the full path
request.encoding
Encoding of data requested for submission
request.session
request. session is a dictionary-like object, which can be read and written. It is often used to save some data to realize session tracking technology. Because HTTP is a stateless, discontinuous protocol.
If you want the server to remember the current access object, you need to record some information about the requester to achieve this goal.
# Settings session
request.session["name"] = "root"
# Get session
name = request.sessin["name"]
! ! ! Here, I need to mention that django will default to session and use json serialization. json serialization can only serialize 1 basic data types, such as numbers, strings, lists, and so on.
So session cannot store objects directly. Simply add SESSION_SERIALIZER to settings = "django. contrib. sessions. serializers. PickleSerializer"
request.COOKIES
Similar to session, the difference is that cookies data is stored on the client side and session data is stored on the server side.
session is relatively more secure, and cookies has different limits on the size of data saved on different browsers. However, existence is reasonable.
cooike has its own advantages.
# Settings cookies
response.set_cookie('name','root')
# Setting Encryption cookies
response.set_cookie('passsword','123456',salt='@#$!%^&')
# Get cookie
request.COOKIES.get("name")
# Gets the encrypted cookie
request.get_signed_cookie("password",salt="@#$!%^&")
request.method
Request Mode POST/GET …
# # # # The following officially starts to obtain data
It is here that I met some small problems, which led me to write this article to record 1
request.body
The body of the request, which returns a string
request.data
The data part of the request, which returns a dictionary object (except that it is very similar to request. body)
request.POST
Get the data submitted in the post form
request.POST["username"]
request.POST.get("username")
request.GET
Get data submitted in get form or url
request.GET["username"]
request.GET.get("username")
Supplement: Detailed explanation of front-end retrieval data under django framework
First of all, the company server uses python language, while our background project uses django framework, and the front-end retrieval data is also based on the framework output. The following is a summary of various situations in which the front-end retrieves data under the framework of django.
The most common ways to retrieve data from front-end pages are summarized as follows:
Data direct output, for cycle output, if judgment output, date formatting.
Let's look at how django outputs for each case
1. Direct data output
{{ form.deliver_institution }}
2. for cycle and if judgment are used in combination
{% for item in form.newborn_hearing_screening %}
{% if forloop.first %}
{% else %}
{{ item }}
{% endif %}
{% endfor %}
3. Date formatting
{{ my_date|date:"Y-m-d" }}