DRF view - request and response

DRF view

drf shorthand code in addition to data serialization reflected outside, in the view are also possible. It django django.views.View class based on the original, internal drf encapsulates many subclasses so that we can use.

The main role of Django REST framwork offers views:

  • Serializer execution control (inspection, storage, data conversion)
  • Execution control database query
  • Class call request and response class [of these two classes is a drf help us once again extend some functional classes.

Request and response

Create a sub-application req

python manage.py startapp httpdemo

Request

REST framework passed in view of the request object is no longer the default Django's HttpRequest object, but extends REST framework provided by the HttpRequest class Request object class.

REST framework provides Parser parser class, after receiving the request according to the request will automatically request the data type specified in the Content-Type (e.g., the JSON, forms, etc.) parse parse request data, parses the class to save dictionary [QueryDict] Object Request object.

Data is the result after the Request object is automatically parsed according to the format of the front end of transmission data.

No matter what format the data sent from the front end, we can read the data in a uniform way.

Common properties

1).data

request.dataRequest body data after parsing is returned. Similar to the standard Django request.POSTand request.FILESproperties, but provides the following features:

  • File and non-file data includes parsing after
  • It contains data of POST, PUT, PATCH request parse
  • Use of parsers parser REST framework, not only to support the type of form data, and also supports JSON data
2).query_params

request.query_paramsAnd the standard Django request.GETsame, just replace the more correct name only.

Response

rest_framework.response.Response

REST framework provides a class response Response, use of such configuration in response to the object, in response to specific data types are converted contents (the render renderer object) to conform to the front end of the demand.

REST framework provides a Renderrenderer, according to the request header Accept(type declaration reception data) automatically converted to the corresponding response data format. If not declared distal Accept request will use the default processing of the response data, we can modify the default response format through configuration.

restframework/settings.py

REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (  # 默认响应渲染类
        'rest_framework.renderers.JSONRenderer',  # json渲染器
        'rest_framework.renderers.BrowsableAPIRenderer',  # 浏览API渲染器
    )
}

Construction way

Response(data, status=None, template_name=None, headers=None, content_type=None)

dataDo data is data after the render process, simply pass the data to the python built-in types, REST framework will use the rendererrenderer process data.

dataData can not be complicated structures, such as Django model class object, for such data we can use Serializerthe sequence of serial process (into the Python dictionaries) before being passed dataparameters.

Parameter Description:

  • data: After preparation of the treatment response sequence of transactions;
  • status: Status, Default 200;
  • template_name: Template name, if HTMLRendererrequired to specify the time;
  • headers: Used to store the dictionary in response to the header information;
  • content_type: Content-Type of the response data, typically this need not pass, REST framework would be required to set the parameters according to the type of data the distal end.

Common properties

1).data

After the response transmitted serialized object, but the data has not been processed render

2).status_code

Digital status codes

3).content

After the data is processed in response to render the

status code

In order to facilitate setting a status code, REST framewrok in rest_framework.statusproviding a common status codes constants module.

1) information to inform - 1xx
HTTP_100_CONTINUE
HTTP_101_SWITCHING_PROTOCOLS
2) Success - 2xx
HTTP_200_OK
HTTP_201_CREATED
HTTP_202_ACCEPTED
HTTP_203_NON_AUTHORITATIVE_INFORMATION
HTTP_204_NO_CONTENT
HTTP_205_RESET_CONTENT
HTTP_206_PARTIAL_CONTENT
HTTP_207_MULTI_STATUS
3) Redirect - 3xx
HTTP_300_MULTIPLE_CHOICES
HTTP_301_MOVED_PERMANENTLY
HTTP_302_FOUND
HTTP_303_SEE_OTHER
HTTP_304_NOT_MODIFIED
HTTP_305_USE_PROXY
HTTP_306_RESERVED
HTTP_307_TEMPORARY_REDIRECT
4) Client Error - 4xx
HTTP_400_BAD_REQUEST
HTTP_401_UNAUTHORIZED
HTTP_402_PAYMENT_REQUIRED
HTTP_403_FORBIDDEN
HTTP_404_NOT_FOUND
HTTP_405_METHOD_NOT_ALLOWED
HTTP_406_NOT_ACCEPTABLE
HTTP_407_PROXY_AUTHENTICATION_REQUIRED
HTTP_408_REQUEST_TIMEOUT
HTTP_409_CONFLICT
HTTP_410_GONE
HTTP_411_LENGTH_REQUIRED
HTTP_412_PRECONDITION_FAILED
HTTP_413_REQUEST_ENTITY_TOO_LARGE
HTTP_414_REQUEST_URI_TOO_LONG
HTTP_415_UNSUPPORTED_MEDIA_TYPE
HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE
HTTP_417_EXPECTATION_FAILED
HTTP_422_UNPROCESSABLE_ENTITY
HTTP_423_LOCKED
HTTP_424_FAILED_DEPENDENCY
HTTP_428_PRECONDITION_REQUIRED
HTTP_429_TOO_MANY_REQUESTS
HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE
HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS
5) Server Error - 5xx
HTTP_500_INTERNAL_SERVER_ERROR
HTTP_501_NOT_IMPLEMENTED
HTTP_502_BAD_GATEWAY
HTTP_503_SERVICE_UNAVAILABLE
HTTP_504_GATEWAY_TIMEOUT
HTTP_505_HTTP_VERSION_NOT_SUPPORTED
HTTP_507_INSUFFICIENT_STORAGE
HTTP_511_NETWORK_AUTHENTICATION_REQUIRED

Guess you like

Origin www.cnblogs.com/jjzz1234/p/11828326.html