vue django project and project interactive supplement, drf introduction, restful specification

A interactive, vue project with django projects

  • The transmission vue ajax request, where the parameters are:
Created () { 
            . Axios the this $ ({ 
                URL: ' http://127.0.0.1:8000/test/ ' , // request address 
                Method: ' POST ' , // request method 
                the params: {   // splicing parameters 
                    a: . 1 
                }, 
                data: {   // data packets 
                    X: 10 
                }, 
                headers: {   // request header 
                    Authorization: ' abc.def.xyz ' , // request header content
                    token: . 1 , 
                    Owen: 2 ,
                     // Background: request.META.get ( ' HTTP_ parameter names all uppercase ' ) 
                } 
            }) the then (Response. => {// the then corresponds success, callback function 
                console.log ( ' success: ' , response.data) 
            }) the catch (error. => {// error trapping 
                the console.log ( ' failed: ' , error.response.data) // error.response.data error 
            }) 
        }

 

  • request of django backend method
request.META   # acquisition request header 

CORS_ORIGIN_ALLOW_ALL = True   # permit cross 

CORS_ALLOW_HEADERS = [   # rewriting CORS_ALLOW_HEADERS, definition allows access request header, the foregoing are the default again plus 
    " Accept " ,
     " Accept-encoding " ,
     " the Authorization " ,
     " Content-of the type " ,
     " DNT " ,
     " Origin " ,
     " the User-Agent " ,
     " the X--csrftoken " ,
    "x-requested-with",

    "token",
    "owen",
]

 

二、drf(Django-restframework)

1. drf main points

1、接口:接口的概念、数据接口文档、接口规范(restful)、Postman接口测试工具

2、drf请求生命周期 - CBV

3、drf的基础组件:请求、响应、渲染、解析、异常

4、drf的序列化(核心):序列化、模型序列化、群操作序列化

5、drf的视图家族:视图类(常用)、视图工具类、工具视图类(常用)、视图集

6、drf的三大认证(核心):认证、权限、频率

7、drf的过滤:筛选、搜索、排序、分页、区间、自定义

2. drf frame mounting

drf框架安装:
    1)drf是Django的插件,所以要提前按照Django
    2)终端中使用命令:python pip install djangorestframework
    3)使用drf时,要在settings中注册

3. web interfaces (WEB API)

什么是web接口:就是 采用某种请求方式提交请求参数,获得响应数据的 url链接
    四部分:url链接 + 请求方式 + 请求参数 + 响应数据
    web接口文档的书写:包含一个接口的访问的url链接的格式和需要的请求参数,以及访问后其响应数据。
    详情见:https://www.cnblogs.com/xiaoyuanqujing/articles/11869745.html

4. restful Interface Specification

url链接:
    1)接口都是操作前后台数据的,所以需要保证数据的安全性
        采用https协议
    2)接口用来操作数据,与网址(操作页面)有区别,所以用特定的关键字表示接口
        api关键字
            - https://api.baidu.com
            - https://www.baidu.com/api
    3)接口操作的数据称之为 资源,在url中只体现 资源 名称(名词),不体现操作资源的方式动词
        常规资源接口
            - https://api.baidu.com/books/
            - https://api.baidu.com/books/(pk)/
            
        非常规接口 - 和某资源不是特别密切或是不止一种资源
            - https://api.baidu.com/login/
            - https://api.baidu.com/place/search/
            
    4)如果一个资源存在多版本结果,在url链接中要用特定符号来兼容多版本共存
        v1|v2
            - https://api.baidu.com/v1/books/
            - https://api.baidu.com/v2/books/
            
    5)群资源操作,一般还有额外的限制条件,如排序、限制调试、分页等等
        ?限制条件
            - https://api.baidu.com/v1/books/?ordering=-price&limit=3


请求方式
    6)五大请求方式
        get:获取单个或多个资源
            - https://api.baidu.com/books/
                群查,返回多个结果对象
            - https://api.baidu.com/books/(pk)/
                单查,返回单个结果对象
                
        post:新增单个或多个资源(都是一个url)
            - https://api.baidu.com/books/
                单增,提交单个数据字典,完成单增,返回单个结果对象
                群增,提供多个数据字典的数组,完成群增,返回多个结果对象
                
        put:整体修改单个或多个资源
            - https://api.baidu.com/books/
                整体修改多个,提供多个数据字典的数组(数据字典中要包含主键),完成群改,返回多个结果对象
            - https://api.baidu.com/books/(pk)/
                整体修改单个,提供单个数据字典(主键在url中体现),完成单改,返回单个结果对象
        
        patch:局部修改单个或多个资源
            方式与put完全相同,不同的是:操作的资源如果有5个key-value键值对,put请求提供的字典必须全包含,但是patch提供的字典包含的键值对0~5个都可以
            
        delete:删除单个或多个资源
            - https://api.baidu.com/books/
                多删,提供多个资源主键数据,完成群删,不做任何资源返回(一般我们会返回结果信息:成功|失败)
            - https://api.baidu.com/books/(pk)/
                单删,不需要提供额外数据,完成单删,不做任何资源返回(一般我们会返回结果信息:成功|失败)
    

响应结果:
    7)响应对象中要包含网络状态码(网络状态信息和网络状态码捆绑出现,不要额外设置):
        1xx:基本信息
        2xx:成功 - 200基本 201新增成功
        3xx:重定向
        4xx:客户端错误 - 400错误请求;403请求无权限;404请求资源不存在
        5xx:服务端错误 - 500服务器错误
        
    8)数据状态码(一般都是前后台约定规则):
        0:成功
        1:失败 - 1xx:具体失败信息(要在接口文档中明确写出)
        2:无数据 - 2xx:具体无数据信息(要在接口文档中明确写出)
        
    9)数据状态信息(一般不仅仅是对数据状态码的解释,更多是对结果的描述,给前台开发者阅读的)
        
    10)数据结果(常量、数组、字典),如果有子资源(图片、音频、视频),返回资源的url链接:
{
            "status": 0,
            "msg": 'ok',
            "results": [{
                "name": "西游记",
                "img": "https://api.baidu.com/media/book/xyj.png"
            }]
        }
 

Three, django model life cycle of CBV

  • django backend start, executing each urls file as_view()method - "
  • Sending a request to the front end of the rear end of the corresponding url-- "
  • The rear end of the request invoke the method corresponding to the class, and then return the results to the front end

Guess you like

Origin www.cnblogs.com/sweet-i/p/12116909.html
Recommended