DRF-- Introduction and Installation

Not separated front and rear ends

Not separated from the rear end of the previous application mode, to see the front page of the effect is controlled by the rear end, the rear end or redirect page rendering, i.e. the front end of the rear end of the display to be controlled, a high degree of coupling of the front and rear ends . This application mode is suitable for pure web application, but when the back-end docking App, App may not require back-end returns an HTML page, but only the data itself, so the back-end web interface had to return does not apply to front-end applications App In order to butt App backend would also need to develop a set of interfaces.

 

Separate front and rear ends

Separating the rear end of the previous application mode, only the data required for the rear end of the front end of return and no longer render HTML pages, no control effect of the front end. As for front-end user to see what effect the requested data from the back end to the front end of how to load, to determine their own by the front-end, web pages there are treatments, there are App App of the treatment, but either the front end, the required data basically the same, only to develop a back-end logic can provide external data.

Separating the rear end of the previous application mode, we typically developed for each view is called a rear end of the interface, or the API, access through the front end interface to the data change search deletions. API has a variety of styles, but now more mainstream and useful herein is to say RESTful API.

RESTful

RESTful is a style of software architecture, design, rather than the standard, but provides a set of design principles and constraints. It is mainly used for client and server interaction class software. Based on this style of software can be more concise, more structured and easier to implement caching mechanisms.

REST stands for Representational State Transfer, Chinese meant to characterize the state transition. It first appeared in 2000, Roy Fielding's doctoral dissertation, Roy Fielding is one of the principal authors of the HTTP specification. He mentioned in the paper: "The purpose of my writing this article, is to, in conformity with the principles of architecture, architecture design to understand and evaluate web-based application software, get a strong function, good performance, suitable for communication architecture .REST refers to a set of architectural constraints and principles. "If a framework and principles consistent with the constraints of REST, we call it RESTful architecture.

REST itself does not create new technologies, components or services, while hiding behind the idea is to use RESTful Web's existing features and capabilities, better use some guidelines and constraints existing Web standards. Although deeply influenced by the REST Web technology itself, but in theory the REST architectural style is not bound to HTTP, but currently only HTTP REST-related instances.

RESTful core operations: URL locate resources with HTTP verbs (GET, POST, DELETE, DETC) description of the operation.

This style interface that what good is it? Front and rear end can be separated. The front end to get the data only responsible for display and rendering, and do not do any data processing. Back-end processing and transmitting data out JSON format is defined so that a uniform interface, the web, ios, android three terminal can use the same interface.

RESTful practice

  • API and user's communication protocol, as far as possible using HTTPs protocol.
  • domain name
    • https://api.example.com best not to use this (there will be cross-domain problems)
    • https://example.org/api/ API is very simple
  • version
    • URL, such as: https: //api.example.com/v1/
    • It included in the request header
  • url, everything is resources, both use nouns denote (can be plural)
    • https://api.example.com/v1/zoos
    • https://api.example.com/v1/animals
    • https://api.example.com/v1/employees
  • method
    • GET: Remove the resource (one or more) from the server
    • POST: Create a new resource on the server
    • PUT: update the server resources (complete resources provided by the client after the change), a full update
    • PATCH: Updated resources (provided by the client to change the properties) in the server, local updates, may not be supported
    • DELETE: delete a resource from the server
  • Filtered, passed through a search condition parameters uploaded in the form url
    • https://api.example.com/v1/zoos?limit=10: Specifies the number of records returned
    • https://api.example.com/v1/zoos?o?set=10: Returns the specified record start position
    • https://api.example.com/v1/zoos?page=2&per_page=100: Specifies the first few pages, as well as the number of records per page
    • https://api.example.com/v1/zoos?sortby=name&order=asc: Specifies which returns the results sorted by attribute, and sort order
    • https://api.example.com/v1/zoos?animal_type_id=1: Specify Filter Conditions
  • + Code status code information
    •  
  • Error processing, when the status code 4xx, an error message should be returned.
    •  
  • Return results for different operations, the results returned by the server to the user should meet the following specifications

Detailed HTTP request method

Request: Specify what the client wants to operate on the specified resource / server as

The standard HTTP, HTTP request can use several request methods.

HTTP1.0 request defines three methods: GET, POST, and HEAD method.

HTTP1.1 five new request methods: OPTIONS, PUT, DELETE, TRACE, and CONNECT method.

 

 

  • GET: access to resources

GET method is used to request resource has been identified URI. Resource specified by the server returns the response parsed content (i.e., if the requested resource is a text, it remains as return; if the CGI [Common Gateway Interface] as procedures, performed after the output of the return) . The most commonly used to query certain information to the server. If necessary, you can append query string parameters to the end of the URL, in order to send information to the server.

  • POST: transport entity text

POST method for transmitting entity body. Although the GET method can also be transmitted by the main entities, but generally do not GET method of transmission, but with the POST method; GET and POST methods, although very similar, but the main purpose is not to obtain POST body content of the response. POST request body may contain a lot of data, but the format is not limited.

  • Fundamentally different methods GET and POST methods:

1, GET method for acquiring information, it is safe (safe: refers to non modification information, such as databases of information), and the POST method is a request to modify the server's resources;

2、GET请求的数据会附在URL之后,而POST方法提交的数据则放置在HTTP报文实体的主体里,所以POST方法的安全性比GET方法要高;

3、GET方法传输的数据量一般限制在2KB,其原因在于:GET是通过URL提交数据,而URL本身对于数据没有限制,但是不同的浏览器对于URL是有限制的,比如IE浏览器对于URL的限制为2KB,而Chrome,FireFox浏览器理论上对于URL是没有限制的,它真正的限制取决于操作系统本身;POST方法对于数据大小是无限制的,真正影响到数据大小的是服务器处理程序的能力。

  • HEAD:获得报文首部

HEAD方法和GET方法一样,只是不返回报文的主体部分,用于确认URI的有效性及资源更新的日期时间等。 具体来说:1、判断类型; 2、查看响应中的状态码,看对象是否存在(响应:请求执行成功了,但无数据返回); 3、测试资源是否被修改过 HEAD方法和GET方法的区别:GET方法有实体,HEAD方法无实体。

  • PUT:传输文件

PUT方法用来传输文件,就像FTP协议的文件上传一样,要求在请求报文的主体中包含文件内容,然后保存在请求URI指定的位置。但是HTTP/1.1的PUT方法自身不带验证机制,任何人都可以上传文件,存在安全问题,故一般不用。

  • POST和PUT的区别

POST 方法用来传输实体的主体,PUT方法用来传输文件,自身不带验证机制。这两个方法看起来都是讲一个资源附加到服务器端的请求,但其实是不一样的。一些狭窄的意见认为,POST方法用来创建资源,而PUT方法则用来更新资源。这个说法本身没有问题,但是并没有从根本上解释了二者的区别。事实上,它们最根本的区别就是:POST方法不是幂等的,而PUT方法则有幂等性。那这又衍生出一个问题,什么是幂等?

幂等(idempotent、idempotence)是一个抽象代数的概念。在计算机中,可以这么理解,一个幂等操作的特点就是其任意多次执行所产生的影响均与依次一次执行的影响相同。POST在请求的时候,服务器会每次都创建一个文件,但是在PUT方法的时候只是简单地更新,而不是去重新创建。因此PUT是幂等的。

  • DELETE:删除资源

指明客户端想让服务器删除某个资源,与PUT方法相反,按URI删除指定资源OPTIONS:询问支持的方法OPTIONS方法用来查询针对请求URI指定资源支持的方法(客户端询问服务器可以提交哪些请求方法)

  • TRACE:追踪路径

客户端可以对请求消息的传输路径进行追踪,TRACE方法是让Web服务器端将之前的请求通信还给客户端的方法

  • CONNECT:要求用隧道协议连接代理

CONNECT方法要求在与代理服务器通信时建立隧道,实现用隧道协议进行TCP通信。主要使用SSL(安全套接层)和TLS(传输层安全)协议把通信内容加密后经网络隧道传输。

DRF

官网地址:https://www.django-rest-framework.org/

Django Rest Framework是一个强大且灵活的工具包,主要用以构建RESTful风格的Web API。Django REST Framework(以后简称DRF)可以在Django的基础上迅速实现API,并且自身还带有基于WEB的测试和浏览页面,可以方便的测试自己的API。DRF几乎是Django生态中进行前后端分离开发的默认库。

Django REST Framework具有以下功能和特性:

  • 自带基于Web的可浏览的API,对于开发者非常有帮助
  • 支持OAuth1a 和OAuth2认证策略
  • 支持ORM或非ORM数据源的序列化
  • 高可自定制性,多种视图类型可选
  • 自动生成符合 RESTful 规范的 API
  • 支持 OPTION、HEAD、POST、GET、PATCH、PUT、DELETE等HTTP方法
  • 根据 Content-Type 来动态的返回数据类型(如HTML、json)
  • 细粒度的权限管理(可到对象级别)
  • 丰富的文档和强大的社区支持
  • Mozilla、Red Hat、 Heroku和Eventbrite等知名公司正在使用

安装:

pip install djangorestframework
pip install markdown    # Markdown
pip install django-filter  # 可选
pip install coreapi      # 可选

coreapi(1.32.0+) - 支持模式生成和coreapi命令行工具。

Markdown(2.1.0+) - 可浏览API的Markdown支持。

django-filter(1.0.1+) - 过滤支持。

django-crispy-forms - 改进的HTML显示过滤。

django-guardian(1.1.1+) - 对象级别的权限支持

安装完毕,在项目配置文件中,注册app:

INSTALLED_APPS = (
 ...
 'rest_framework',
)

 

Guess you like

Origin www.cnblogs.com/zouzou-busy/p/11628700.html