python之Django rest_framework总结

一、rest api

python之Django rest_framework总结,

一、rest api

   a、api正是接口

        如: –

               –

   b、api的七个用途

        1、为别人提供劳动

        2、前后端分离

二、restful

    a、–字面意思:表征状态转移

    b、面向能源编制程序,对互连网上的任意东西都视为资源

         如:-

                –

三、restful规范

     a、 —URL

     b、 —url名词

路径,视网络上任何东西都是资源,均使用名词表示(可复数)
  https://api.example.com/v1/zoos
  https://api.example.com/v1/animals
  https://api.example.com/v1/employee

    c、 —status状态码**

200 OK - [GET]:服务器成功返回用户请求的数据,该操作是幂等的(Idempotent)。
201 CREATED - [POST/PUT/PATCH]:用户新建或修改数据成功。
202 Accepted - [*]:表示一个请求已经进入后台排队(异步任务)
204 NO CONTENT - [DELETE]:用户删除数据成功。
400 INVALID REQUEST - [POST/PUT/PATCH]:用户发出的请求有错误,服务器没有进行新建或修改数据的操作,该操作是幂等的。
401 Unauthorized - [*]:表示用户没有权限(令牌、用户名、密码错误)。
403 Forbidden - [*] 表示用户得到授权(与401错误相对),但是访问是被禁止的。
404 NOT FOUND - [*]:用户发出的请求针对的是不存在的记录,服务器没有进行操作,该操作是幂等的。
406 Not Acceptable - [GET]:用户请求的格式不可得(比如用户请求JSON格式,但是只有XML格式)。
410 Gone -[GET]:用户请求的资源被永久删除,且不会再得到的。
422 Unprocesable entity - [POST/PUT/PATCH] 当创建一个对象时,发生一个验证错误。
500 INTERNAL SERVER ERROR - [*]:服务器发生错误,用户将无法判断发出的请求是否成功。

   d、—提交格局

GET      :从服务器取出资源(一项或多项)
POST    :在服务器新建一个资源
PUT      :在服务器更新资源(客户端提供改变后的完整资源)
PATCH  :在服务器更新资源(客户端提供改变的属性)
DELETE :从服务器删除资源

   e、  —错误音讯

状态码是4xx时,应返回错误信息,error当做key。
{
    error: "Invalid API key"
}

   f、 —版本**

   g、 —Hypermedia link,RESTful
API最佳成功Hypermedia,即重返结果中提供链接,连向其余API方法,使得用户不查文书档案,也通晓下一步应该做哪些。

   h、—域名**

   j、—过滤,通过在url上传参的样式传递搜索条件**

 
k、 
—再次来到结果,针对不一致操作,服务器向用户重回的结果应当符合以下标准**

GET /collection:返回资源对象的列表(数组)  
GET /collection/resource:返回单个资源对象
POST /collection:返回新生成的资源对象
PUT /collection/resource:返回完整的资源对象
PATCH /collection/resource:返回完整的资源对象
DELETE /collection/resource:返回一个空文档

rest_Framework的规范:

   
按顺序:它的method的分裂,原来未有思考,原来是url区分,将来通过method来分别,method的两样提交格局各异,紧接着壹般是面向能源的正是把url变成名词,接下正是重回值,从前尚未思考状态码,以后有考虑状态码。(一般有get,post方法,还有put,delete等方法)

python之Django rest_framework总结。**四、 基于Django Rest Framework框架完毕 **

   a、安装:pip3 install djangorestframework -i
–trusted-host=pypi.douban.com

五、什么是RESTful

  • REST与技术非亲非故,代表的是一种软件架构风格,REST是Representational
    State Transfer的简称,普通话翻译为“表征状态转移”
  • REST从财富的角度类审视整个网络,它将遍布在网络中某些节点的财富通过U奥德赛L进行标识,客户端应用通过UEnclaveL来博取财富的性状,得到那么些特征致使那么些应用转变状态
  • REST与技能非亲非故,代表的是一种软件架构风格,REST是Representational
    State Transfer的简称,汉语翻译为“表征状态转移”
  • 怀有的数目,不过是经过网络获取的依然操作(增加和删除改查)的数额,都以能源,将全数数据正是财富是REST差别与别的架构风格的最本质属性
  • 对此REST这种面向能源的架构风格,有人提出壹种崭新的结构理念,即:面向能源框架结构(ROA:Resource
    Oriented Architecture)

      a、 django的中间件比rest_framework执行的早**

      b、 认证的功力放到中间件也是足以做的**

     
c、
说美素佳儿般做,检查用户是或不是存在,要是存在request.user/request.auth;不存在request.user/request.auth=None**

      d、表达小计算:**

  ---类:authenticate/authenticate_header

      ---返回值:None,元组(user,auth),异常

      ---配置:

                 ---视图:                      

                                    class IndexView(APIView):
                                           authentication_classes = [MyAuthentication,]

                 ---全局                    

                                    REST_FRAMEWORK = {
                                                'UNAUTHENTICATED_USER': None,
                                                'UNAUTHENTICATED_TOKEN': None,
                                                "DEFAULT_AUTHENTICATION_CLASSES": [
                                                 # "app02.utils.MyAuthentication",
                                                ],
                                               }

六、权限

   
a、
权力才真正的做request.user/request.auth得到它们做是或不是访问的论断**

    b、权限小总计: **

     ---类:       has_permission/has_object_permission

     ---返回值:True、False、exceptions.PermissionDenied(detail="错误信息")

     ---配置:

               ---视图:                  

                                class IndexView(APIView):
                                       permission_classes = [MyPermission,]

                      ---全局:

                                REST_FRAMEWORK = {
                                "DEFAULT_PERMISSION_CLASSES": [
                                # "app02.utils.MyAuthentication",
                                 ],
                               }

7、限制访问的作用

    限制访问频率的运用:

   a、对匿名用户举行界定,每一种用户一分钟允许访问十二回

        在此处用唯一标识:self.get_ident() 

   b、限定访问的功效小计算: **

  ---类:       allow_request/wait PS: scope = "wdp_user"

     ---返回值:True、False

     ---配置:

                  ---视图:

                                            class IndexView(APIView):
                                                     throttle_classes=[AnonThrottle,UserThrottle,]
                                                     def get(self,request,*args,**kwargs):
                                                     self.dispatch
                                                     return Response('访问首页')

                   ---全局:

                                          REST_FRAMEWORK = {
                                                      "DEFAULT_THROTTLE_CLASSES":[
                                                   ],
                                                      'DEFAULT_THROTTLE_RATES':{
                                                      'wdp_anon':'5/minute',
                                                      'wdp_user':'10/minute',
                                                   }
                                                   }

、认证、权限、限制访问频率重回结果的可比

    1、申明、权限、限制访问频率那多少个重临的结果:**

**     a、注解再次来到的三种结果:             a、None             b、元组(user,auth)             c、异常 raise APIException(…)**

**     b、 权限的重临值二种结果:**

**            a、True 有权限             b、False 没权限             c、异常**

**     c、 限制访问的频率重临值的三种结果:**

**            a、True 
            b、False**

实例化:
v1 = ["view.xxx.path.Role","view.xxx.path.Group",]    可以循环,循环出来的每一个不能实例化
如果把v1循环弄成每一个对象列表,通过rsplit切割,在通过importlib.import_module拿到每一个路径,在通过getattr把它的类名拿过来,
这个类加括号就是实例化想
for item in v1:
m = importlib.import_module('view.xxx.path')
cls = getattr(m,'Role')
cls()



from view.xxx.path import Role,Group      
v2 = [Group,Role]      这个可以循环每一个实例化
for item in v2:     #循环V2的每一个元素加括号,就是实例化
item()

九、Django rest_Framework框架

     a、  —-为啥用Django rest_Framework框架?**

----首先没有Django rest_Framework框架用django也是可以做出来的,只不过它为我们提供一些API常用的功能,比如:(认证,权限,限流,有了这些我们只需要写个类已配置,
它就能当都市图用,还能全局配置,如果自己写还得写中间件,写装饰器来实现,通过Django rest_Framework框架,他已经把规则写好,只需要写类,只需实现方法,返回值就可以)
实现了一部分功能。

  ----设计比较好

            ----单独视图+全局配置 =>Dajngo中间件(importlib/反射)=>动态配置课扩展(短信,邮件,微信等提醒)

     b、Django rest_Framework原理?

             先开头在路由,路由.as_view,

             点击as_view**

**             请求进入,走完以上,才走self.dispatch()**

           
 走
self.dispatch()流程如下地址: 

十、版本

**    a、依据url的例外来来操作,版本控制**

**          先在setting中注册**

**十一、rest framework解析器**

**   
a、伸手的数目开始展览解析:请求体进行分析。表示服务端能够分析的数额格式的项目。**

Content-Type: application/url-encoding.....
            request.body
            request.POST

            Content-Type: application/json.....
            request.body
            request.POST

        客户端:
            Content-Type: application/json
            '{"name":"alex","age":123}'

        服务端接收:
            读取客户端发送的Content-Type的值 application/json

            parser_classes = [JSONParser,]
            media_type_list = ['application/json',]

            如果客户端的Content-Type的值和 application/json 匹配:JSONParser处理数据
            如果客户端的Content-Type的值和 application/x-www-form-urlencoded 匹配:FormParser处理数据


        配置:
            单视图:
            class UsersView(APIView):
                parser_classes = [JSONParser,]

            全局配置:
                REST_FRAMEWORK = {
                    'VERSION_PARAM':'version',
                    'DEFAULT_VERSION':'v1',
                    'ALLOWED_VERSIONS':['v1','v2'],
                    # 'DEFAULT_VERSIONING_CLASS':"rest_framework.versioning.HostNameVersioning"
                    'DEFAULT_VERSIONING_CLASS':"rest_framework.versioning.URLPathVersioning",
                    'DEFAULT_PARSER_CLASSES':[
                        'rest_framework.parsers.JSONParser',
                        'rest_framework.parsers.FormParser',
                    ]
                }

十二、 rest framework序列化+Form

     a、序列化:

               对象 -> 字符串 序列化                字符串 -> 对象 反系列化**

**     b、目的:                解决QuerySet体系化难点**

**十三、为啥要内外端分离**

**    a、因为前者它有谈得来框架,那样它的效用就十分高**

**   
b、不做上下端分离,集团只要既有客户端,又有app这种情状下你就的写四次**

**十四、django restful框架好处**

  帮助我们写了广大组件比如:

   
 a、认证:有类,类中的方法
authenticate/authenticate_header,它的再次来到值有None,元组,相当。如果重临值为None那就不管,它是匿名用户。**

     b、权限:有类,类中的方法:has_permission**

   
 c、节流:有类,类的章程:allow_request,在这里allow_request来代表限制,它是因此IP来界定,

 它的内部原理是:默认是IP,用户来用IP,这个IP可能代理IP,IP或者是代理IP拿着请求头默认放到大家能够的缓存中                          
去,每一个人的IP为p,后面的那个列表存它的方位时间,每一次请求进来获取它的当前时间根据时间的个数来比较,在                        
比较的过程中看看把不符合时间的个数来进行比较,能访问就继续,不能访问就不要继续。 

   
 d、版本:是url和hostname,他们八个钟每2个都有五个格局贰个是帮你拿版本另一个是帮您反向生成url

     e、解析器:用户发过来的请求体数据实行操作

     f、类别化:多个职能:连串化,校验

十五、分页

分页的三种境况:

       a、记录当前访问页的多少id

       b、最多展现120页

       c、对页码进行加密

a、基于limit offset做分页

class P1(LimitOffsetPagination):

                max_limit = 3
                default_limit = 2
                limit_query_param = 'limit'
                offset_query_param = 'offset'


            class IndexView(views.APIView):
                def get(self,request,*args,**kwargs):
                    user_list = models.UserInfo.objects.all()
                    p1 = P1()
                    page_user_list = p1.paginate_queryset(queryset=user_list, request=request, view=self)
                    ser = IndexSerializer(instance=page_user_list, many=True)
                    return Response(ser.data) # 不含上一页和下一页
                    # return p1.get_paginated_response(ser.data) # 含上一页和下一页

            class IndexView(views.APIView):
                def get(self,request,*args,**kwargs):
                    ret = BaseResponse()
                    try:
                        user_list = models.UserInfo.objects.all()
                        p1 = P1()
                        page_user_list = p1.paginate_queryset(queryset=user_list,request=request,view=self)
                        ser = IndexSerializer(instance=page_user_list,many=True)
                        ret.data = ser.data
                        ret.next = p1.get_next_link()
                    except Exception as e:
                        ret.code= 1001
                        ret.error = 'xxxx错误'
                    return Response(ret.__dict__)

 b. 基于页码的分页

class P2(PageNumberPagination):
                # 每页显示的数据条数
                max_page_size = 5
                page_size = 2
                page_size_query_param = 'size'

                # 页码
                page_query_param = 'page'

十六、视图

1、APIView

class IndexView(views.APIView):
                    def get(self, request, *args, **kwargs):
                        user_list = models.UserInfo.objects.all()
                        ser = IndexSerializer(instance=user_list,many=True)
                        return Response(ser.data)

2、GenericAPIview(APIView)

3、GenericViewSet(ViewSetMixin,generics.GenericAPIView)

路由修改:
                    urlpatterns = [
                        url(r'^index/$', views.IndexView.as_view({'get':'list','post':'create'})),
                        url(r'^index/(?P<pk>\d+)$', views.IndexView.as_view({'get':'retrieve'})),
                    ]

                视图修改:

                    class IndexView(viewsets.GenericViewSet):

                        def list(self,request,*args,**kwargs):

                             pass # 获取列表信息

                        def retrieve(self, request, *args, **kwargs):
                            pass  # 获取单条数据

                        def create(self,request, *args, **kwargs):
                            pass


                自定义:

                        增
                            POST
                            /users/
                        删
                            DELETE
                            /users/1/
                        改
                            PUT
                            /users/1/

                            patch
                            /users/1/
                        查
                            GET
                            /users/ 
                            GET
                            /users/1/

                        urlpatterns = [

                            url(r'^index/$', views.IndexView.as_view()),
                            url(r'^index/(?P<pk>\d+)$', views.IndexView.as_view()),
                        ]

                        class IndexView(views.APIView):

                            def get(self,request,*args,**kwargs):
                                pk = kwargs.get('pk')
                                if pk:
                                    pass # 获取单条信息
                                else:
                                    pass # 获取列表信息

                            def post(self,request,*args,**kwargs):
                                pass

                            def put(self,request,*args,**kwargs):
                                pass

                            def patch(self,request,*args,**kwargs):
                                pass

                            def delete(self,request,*args,**kwargs):
                                pass

4、ModelViewSet

ModelViewSet(mixins.CreateModelMixin,mixins.RetrieveModelMixin,mixins.UpdateModelMixin,mixins.DestroyModelMixin,mixins.ListModelMixin,GenericViewSet)                

                class IndexView(ModelViewSet):

十七、 渲染器
    看到的页面时怎样子的,重返数据。
    renderer_classes = [JSONRenderer,BrowsableAPIRenderer]

十八、跨域

   a、  浏览器的同源策略

              一、  —-对ajax请求举行阻挠

               二、 —-对href属性读不阻拦

       xhr=new XMLHttpRequest

       xhr.open…

       xhr.send(…)

    b、 消除方案:

         —JSONP

                 点击按钮: 

                        动态增进三个

<script src='http://www.baidu.com/users/'></script>
                        <script>
                            function func(arg){
                                alert(arg)
                            }
                        </script

    c、删除

<script src='http://www.baidu.com/users/'></script>

 

rest_framework计算, 1、rest api
a、api就是接口 如: – –
b、api的多少个用途…

Django Rest Framework

Django Rest Framework

 

   a、api正是接口

一、什么是RESTful

  • REST与技术无关,代表的是壹种软件架构风格,REST是Representational
    State Transfer的简称,汉语翻译为“表征状态转移”

  • REST从财富的角度类审视整个互联网,它将分布在互联网中有些节点的财富通过UHighlanderL举行标识,客户端应用通过ULX570L来赢得能源的特色,得到这几个特点致使那一个应用转变状态

  • 拥有的数目,但是是经过互连网获得的或许操作(增加和删除改查)的数码,都是财富,将全部数据就是能源是REST差异与此外架构风格的最本质属性

  • 对此REST那种面向能源的架构风格,有人提议壹种崭新的构造理念,即:面向能源架构(ROA:Resource
    Oriented Architecture)

一. 什么是RESTful 

  • REST与技能无关,代表的是1种软件架构风格,REST是Representational
    State Transfer的简称,汉译为“表征状态转移”
  • REST从财富的角度类审视整个网络,它将遍布在网络中某些节点的能源通过U福特ExplorerL进行标识,客户端应用通过URL来取得能源的特征,获得那么些特点致使那几个使用转变状态
  • REST与技能无关,代表的是一种软件架构风格,REST是Representational
    State Transfer的简称,中文翻译为“表征状态转移”
  • 享有的数额,可是是因此网络获取的要么操作(增加和删除改查)的多少,都是财富,将总体数据正是能源是REST差异与其他架构风格的最本质属性
  • 对此REST那种面向财富的架构风格,有人提议一种崭新的组织理念,即:面向财富架构(ROA:Resource
    Oriented Architecture)

        如: –

二、RESTful API设计

API与用户的通讯协议,总是利用HTTPs协议。

域名

  • 尽量将API布置在专用域名(会设有跨域难题)

  • API很简单

版本

  • URL,如:

  • 伸手头 跨域时,引发发送数次请求

路线,视互连网上其它交事务物都以财富,均运用名词意味着(可复数)

method

  • GET :从服务器取出能源(壹项或多项)
  • POST :在服务器新建二个财富
  • PUT :在服务器更新财富(客户端提供改变后的全部能源)
  • PATCH :在服务器更新能源(客户端提供改变的习性)
  • DELETE :从服务器删除财富

过滤,通过在url上传参的格局传递搜索条件

  • https://api.example.com/v1/zoos?limit=拾:钦命重回记录的数量
  • https://api.example.com/v1/zoos?offset=拾:内定重临记录的启幕地点
  • https://api.example.com/v1/zoos?page=2&per\_page=100:钦赐第几页,以及每页的记录数
  • https://api.example.com/v1/zoos?sortby=name&order=asc:内定重临结果遵照哪个属性排序,以及排序依次
  • https://api.example.com/v1/zoos?animal\_type\_id=1:钦定筛选标准

状态码

200 OK - [GET]:服务器成功返回用户请求的数据,该操作是幂等的(Idempotent)。
201 CREATED - [POST/PUT/PATCH]:用户新建或修改数据成功。
202 Accepted - [*]:表示一个请求已经进入后台排队(异步任务)
204 NO CONTENT - [DELETE]:用户删除数据成功。
400 INVALID REQUEST - [POST/PUT/PATCH]:用户发出的请求有错误,服务器没有进行新建或修改数据的操作,该操作是幂等的。
401 Unauthorized - [*]:表示用户没有权限(令牌、用户名、密码错误)。
403 Forbidden - [*] 表示用户得到授权(与401错误相对),但是访问是被禁止的。
404 NOT FOUND - [*]:用户发出的请求针对的是不存在的记录,服务器没有进行操作,该操作是幂等的。
406 Not Acceptable - [GET]:用户请求的格式不可得(比如用户请求JSON格式,但是只有XML格式)。
410 Gone -[GET]:用户请求的资源被永久删除,且不会再得到的。
422 Unprocesable entity - [POST/PUT/PATCH] 当创建一个对象时,发生一个验证错误。
500 INTERNAL SERVER ERROR - [*]:服务器发生错误,用户将无法判断发出的请求是否成功。

更多看这里:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

错误处理,状态码是四xx时,应重回错误新闻,error当做key。

{
    error: "Invalid API key"
}

回来结果,针对差异操作,服务器向用户再次回到的结果应该符合以下标准。

GET /collection:返回资源对象的列表(数组)
GET /collection/resource:返回单个资源对象
POST /collection:返回新生成的资源对象
PUT /collection/resource:返回完整的资源对象
PATCH /collection/resource:返回完整的资源对象
DELETE /collection/resource:返回一个空文档

Hypermedia API,RESTful
API最棒完毕Hypermedia,即重临结果中提供链接,连向任何API方法,使得用户不查文书档案,也知晓下一步应该做怎么样。

{"link": {
  "rel":   "collection https://www.example.com/zoos",
  "href":  "https://api.example.com/zoos",
  "title": "List of zoos",
  "type":  "application/vnd.yourformat+json"
}}

摘自:

三、基于Django实现

路由系统:

urlpatterns = [
    url(r'^users', Users.as_view()),
]

CBV视图:

from django.views import View
from django.http import JsonResponse

class Users(View):
    def get(self, request, *args, **kwargs):
        result = {
            'status': True,
            'data': 'response data'
        }
        return JsonResponse(result, status=200)

    def post(self, request, *args, **kwargs):
        result = {
            'status': True,
            'data': 'response data'
        }
        return JsonResponse(result, status=200)

二. RESTful API设计

  • API与用户的通讯协议,总是利用HTTPs协议。
  • 域名 
    •  
                           
       尽量将API陈设在专用域名(会存在跨域难点)
    •  
                            API很简单
  • 版本
    • URL,如:
    • 请求头           
                                           
      跨域时,引发发送数次呼吁
  • 路线,视互联网上任王辉西都以能源,均使用名词意味着(可复数)
  • method
    • GET     
      :从服务器取出能源(一项或多项)
    • POST   
      :在服务器新建2个能源
    • PUT     
      :在服务器更新能源(客户端提供改变后的完好财富)
    • PATCH 
      :在服务器更新能源(客户端提供改变的属性)
    • DELETE
      :从服务器删除财富
  • 过滤,通过在url上传参的款式传递搜索条件
  • 状态码
    美高梅开户网址 1美高梅开户网址 2

    200 OK - [GET]:服务器成功返回用户请求的数据,该操作是幂等的(Idempotent)。
    201 CREATED - [POST/PUT/PATCH]:用户新建或修改数据成功。
    202 Accepted - [*]:表示一个请求已经进入后台排队(异步任务)
    204 NO CONTENT - [DELETE]:用户删除数据成功。
    400 INVALID REQUEST - [POST/PUT/PATCH]:用户发出的请求有错误,服务器没有进行新建或修改数据的操作,该操作是幂等的。
    401 Unauthorized - [*]:表示用户没有权限(令牌、用户名、密码错误)。
    403 Forbidden - [*] 表示用户得到授权(与401错误相对),但是访问是被禁止的。
    404 NOT FOUND - [*]:用户发出的请求针对的是不存在的记录,服务器没有进行操作,该操作是幂等的。
    406 Not Acceptable - [GET]:用户请求的格式不可得(比如用户请求JSON格式,但是只有XML格式)。
    410 Gone -[GET]:用户请求的资源被永久删除,且不会再得到的。
    422 Unprocesable entity - [POST/PUT/PATCH] 当创建一个对象时,发生一个验证错误。
    500 INTERNAL SERVER ERROR - [*]:服务器发生错误,用户将无法判断发出的请求是否成功。
    
    更多看这里:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
    

    常用状态码列表

  • 错误处理,状态码是四xx时,应再次来到错误新闻,error当做key。

    {
        error: "Invalid API key"
    }
    
  • 重临结果,针对不相同操作,服务器向用户重返的结果应当符合以下标准。

    GET /collection:返回资源对象的列表(数组)
    GET /collection/resource:返回单个资源对象
    POST /collection:返回新生成的资源对象
    PUT /collection/resource:返回完整的资源对象
    PATCH /collection/resource:返回完整的资源对象
    DELETE /collection/resource:返回一个空文档
    
  • Hypermedia API,RESTful
    API最佳成功Hypermedia,即再次来到结果中提供链接,连向其它API方法,使得用户不查文书档案,也知道下一步应该做什么样。

    {"link": {
      "rel":   "collection https://www.example.com/zoos",
      "href":  "https://api.example.com/zoos",
      "title": "List of zoos",
      "type":  "application/vnd.yourformat+json"
    }}
    

  摘自: 

               –

肆、基于Django Rest Framework框架达成

三. 基于Django实现

路由系统:

urlpatterns = [
    url(r'^users', Users.as_view()),
]

CBV视图:

from django.views import View
from django.http import JsonResponse

class Users(View):
    def get(self, request, *args, **kwargs):
        result = {
            'status': True,
            'data': 'response data'
        }
        return JsonResponse(result, status=200)

    def post(self, request, *args, **kwargs):
        result = {
            'status': True,
            'data': 'response data'
        }
        return JsonResponse(result, status=200) 

   b、api的三个用途

1、基本流程

url.py

from django.conf.urls import url, include
from web.views.s1_api import TestView

urlpatterns = [
    url(r'^test/', TestView.as_view()),
]

views.py

from rest_framework.views import APIView
from rest_framework.response import Response


class TestView(APIView):
    def dispatch(self, request, *args, **kwargs):
        """
        请求到来之后,都要执行dispatch方法,dispatch方法根据请求方式不同触发 get/post/put等方法

        注意:APIView中的dispatch方法有好多好多的功能
        """
        return super().dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

上述是rest
framework框架基本流程,重要的效率是在APIView的dispatch中触发。

4. 基于Django Rest Framework框架实现

        壹、为外人提供服务

二.声明和授权

a. 用户url传入的token认证

  • urls.py

    from django.conf.urls import url, include
    from web.viewsimport TestView
    
    urlpatterns = [
        url(r'^test/', TestView.as_view()),
    ]
    
  • views.py

    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.authentication import BaseAuthentication
    from rest_framework.request import Request
    from rest_framework import exceptions
    
    token_list = [
        'sfsfss123kuf3j123',
        'asijnfowerkkf9812',
    ]
    
    class TestAuthentication(BaseAuthentication):
        def authenticate(self, request):
            """
            用户认证,如果验证成功后返回元组: (用户,用户Token)
            :param request: 
            :return: 
                None,表示跳过该验证;
                    如果跳过了所有认证,默认用户和Token和使用配置文件进行设置
                    self._authenticator = None
                    if api_settings.UNAUTHENTICATED_USER:
                        self.user = api_settings.UNAUTHENTICATED_USER()
                    else:
                        self.user = None

                    if api_settings.UNAUTHENTICATED_TOKEN:
                        self.auth = api_settings.UNAUTHENTICATED_TOKEN()
                    else:
                        self.auth = None
                (user,token)表示验证通过并设置用户名和Token;
                AuthenticationFailed异常
            """
            val = request.query_params.get('token')
            if val not in token_list:
                raise exceptions.AuthenticationFailed("用户认证失败")

            return ('登录用户', '用户token')

        def authenticate_header(self, request):
            """
            Return a string to be used as the value of the `WWW-Authenticate`
            header in a `401 Unauthenticated` response, or `None` if the
            authentication scheme should return `403 Permission Denied` responses.
            """
            # 验证失败时,返回的响应头WWW-Authenticate对应的值
            pass


    class TestView(APIView):
        authentication_classes = [TestAuthentication, ]
        permission_classes = []

        def get(self, request, *args, **kwargs):
            print(request.user)
            print(request.auth)
            return Response('GET请求,响应内容')

        def post(self, request, *args, **kwargs):
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

b. 请求头认证

  • urls.py

    from django.conf.urls import url, include
    from web.viewsimport TestView
    
    urlpatterns = [
        url(r'^test/', TestView.as_view()),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.authentication import BaseAuthentication
    from rest_framework.request import Request
    from rest_framework import exceptions
    
    token_list = [
        'sfsfss123kuf3j123',
        'asijnfowerkkf9812',
    ]
    
    class TestAuthentication(BaseAuthentication):
        def authenticate(self, request):
            """
            用户认证,如果验证成功后返回元组: (用户,用户Token)
            :param request: 
            :return: 
                None,表示跳过该验证;
                    如果跳过了所有认证,默认用户和Token和使用配置文件进行设置
                    self._authenticator = None
                    if api_settings.UNAUTHENTICATED_USER:
                        self.user = api_settings.UNAUTHENTICATED_USER()
                    else:
                        self.user = None

                    if api_settings.UNAUTHENTICATED_TOKEN:
                        self.auth = api_settings.UNAUTHENTICATED_TOKEN()
                    else:
                        self.auth = None
                (user,token)表示验证通过并设置用户名和Token;
                AuthenticationFailed异常
            """
            import base64
            auth = request.META.get('HTTP_AUTHORIZATION', b'')
            if auth:
                auth = auth.encode('utf-8')
            auth = auth.split()
            if not auth or auth[0].lower() != b'basic':
                raise exceptions.AuthenticationFailed('验证失败')
            if len(auth) != 2:
                raise exceptions.AuthenticationFailed('验证失败')
            username, part, password = base64.b64decode(auth[1]).decode('utf-8').partition(':')
            if username == 'alex' and password == '123':
                return ('登录用户', '用户token')
            else:
                raise exceptions.AuthenticationFailed('用户名或密码错误')

        def authenticate_header(self, request):
            """
            Return a string to be used as the value of the `WWW-Authenticate`
            header in a `401 Unauthenticated` response, or `None` if the
            authentication scheme should return `403 Permission Denied` responses.
            """
            return 'Basic realm=api'


    class TestView(APIView):
        authentication_classes = [TestAuthentication, ]
        permission_classes = []

        def get(self, request, *args, **kwargs):
            print(request.user)
            print(request.auth)
            return Response('GET请求,响应内容')

        def post(self, request, *args, **kwargs):
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

c.八个申明规则

  • urls.py

    from django.conf.urls import url, include
    from web.views.s2_auth import TestView

    urlpatterns = [
    url(r’^test/’, TestView.as_view()),]

  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.authentication import BaseAuthentication
    from rest_framework.request import Request
    from rest_framework import exceptions
    
    token_list = [
        'sfsfss123kuf3j123',
        'asijnfowerkkf9812',
    ]
    
    class Test1Authentication(BaseAuthentication):
        def authenticate(self, request):
            """
            用户认证,如果验证成功后返回元组: (用户,用户Token)
            :param request: 
            :return: 
                None,表示跳过该验证;
                    如果跳过了所有认证,默认用户和Token和使用配置文件进行设置
                    self._authenticator = None
                    if api_settings.UNAUTHENTICATED_USER:
                        self.user = api_settings.UNAUTHENTICATED_USER() # 默认值为:匿名用户
                    else:
                        self.user = None

                    if api_settings.UNAUTHENTICATED_TOKEN:
                        self.auth = api_settings.UNAUTHENTICATED_TOKEN()# 默认值为:None
                    else:
                        self.auth = None
                (user,token)表示验证通过并设置用户名和Token;
                AuthenticationFailed异常
            """
            import base64
            auth = request.META.get('HTTP_AUTHORIZATION', b'')
            if auth:
                auth = auth.encode('utf-8')
            else:
                return None
            print(auth,'xxxx')
            auth = auth.split()
            if not auth or auth[0].lower() != b'basic':
                raise exceptions.AuthenticationFailed('验证失败')
            if len(auth) != 2:
                raise exceptions.AuthenticationFailed('验证失败')
            username, part, password = base64.b64decode(auth[1]).decode('utf-8').partition(':')
            if username == 'alex' and password == '123':
                return ('登录用户', '用户token')
            else:
                raise exceptions.AuthenticationFailed('用户名或密码错误')

        def authenticate_header(self, request):
            """
            Return a string to be used as the value of the `WWW-Authenticate`
            header in a `401 Unauthenticated` response, or `None` if the
            authentication scheme should return `403 Permission Denied` responses.
            """
            # return 'Basic realm=api'
            pass

    class Test2Authentication(BaseAuthentication):
        def authenticate(self, request):
            """
            用户认证,如果验证成功后返回元组: (用户,用户Token)
            :param request: 
            :return: 
                None,表示跳过该验证;
                    如果跳过了所有认证,默认用户和Token和使用配置文件进行设置
                    self._authenticator = None
                    if api_settings.UNAUTHENTICATED_USER:
                        self.user = api_settings.UNAUTHENTICATED_USER() # 默认值为:匿名用户
                    else:
                        self.user = None

                    if api_settings.UNAUTHENTICATED_TOKEN:
                        self.auth = api_settings.UNAUTHENTICATED_TOKEN()# 默认值为:None
                    else:
                        self.auth = None
                (user,token)表示验证通过并设置用户名和Token;
                AuthenticationFailed异常
            """
            val = request.query_params.get('token')
            if val not in token_list:
                raise exceptions.AuthenticationFailed("用户认证失败")

            return ('登录用户', '用户token')

        def authenticate_header(self, request):
            """
            Return a string to be used as the value of the `WWW-Authenticate`
            header in a `401 Unauthenticated` response, or `None` if the
            authentication scheme should return `403 Permission Denied` responses.
            """
            pass


    class TestView(APIView):
        authentication_classes = [Test1Authentication, Test2Authentication]
        permission_classes = []

        def get(self, request, *args, **kwargs):
            print(request.user)
            print(request.auth)
            return Response('GET请求,响应内容')

        def post(self, request, *args, **kwargs):
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

d.认证和权杖

  • urls.py

    from django.conf.urls import url, include
    from web.views import TestView

    urlpatterns = [
    url(r’^test/’, TestView.as_view()),]

  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.authentication import BaseAuthentication
    from rest_framework.permissions import BasePermission
    
    from rest_framework.request import Request
    from rest_framework import exceptions
    
    token_list = [
        'sfsfss123kuf3j123',
        'asijnfowerkkf9812',
    ]
    
    class TestAuthentication(BaseAuthentication):
        def authenticate(self, request):
            """
            用户认证,如果验证成功后返回元组: (用户,用户Token)
            :param request: 
            :return: 
                None,表示跳过该验证;
                    如果跳过了所有认证,默认用户和Token和使用配置文件进行设置
                    self._authenticator = None
                    if api_settings.UNAUTHENTICATED_USER:
                        self.user = api_settings.UNAUTHENTICATED_USER() # 默认值为:匿名用户
                    else:
                        self.user = None

                    if api_settings.UNAUTHENTICATED_TOKEN:
                        self.auth = api_settings.UNAUTHENTICATED_TOKEN()# 默认值为:None
                    else:
                        self.auth = None
                (user,token)表示验证通过并设置用户名和Token;
                AuthenticationFailed异常
            """
            val = request.query_params.get('token')
            if val not in token_list:
                raise exceptions.AuthenticationFailed("用户认证失败")

            return ('登录用户', '用户token')

        def authenticate_header(self, request):
            """
            Return a string to be used as the value of the `WWW-Authenticate`
            header in a `401 Unauthenticated` response, or `None` if the
            authentication scheme should return `403 Permission Denied` responses.
            """
            pass


    class TestPermission(BasePermission):
        message = "权限验证失败"

        def has_permission(self, request, view):
            """
            判断是否有权限访问当前请求
            Return `True` if permission is granted, `False` otherwise.
            :param request: 
            :param view: 
            :return: True有权限;False无权限
            """
            if request.user == "管理员":
                return True

        # GenericAPIView中get_object时调用
        def has_object_permission(self, request, view, obj):
            """
            视图继承GenericAPIView,并在其中使用get_object时获取对象时,触发单独对象权限验证
            Return `True` if permission is granted, `False` otherwise.
            :param request: 
            :param view: 
            :param obj: 
            :return: True有权限;False无权限
            """
            if request.user == "管理员":
                return True


    class TestView(APIView):
        # 认证的动作是由request.user触发
        authentication_classes = [TestAuthentication, ]

        # 权限
        # 循环执行所有的权限
        permission_classes = [TestPermission, ]

        def get(self, request, *args, **kwargs):
            # self.dispatch
            print(request.user)
            print(request.auth)
            return Response('GET请求,响应内容')

        def post(self, request, *args, **kwargs):
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

e. 全局使用

上述操作中均是对单独视图进行分外布署,倘使想要对全局举办布局,则必要再布局文件中写入即可

  • settings.py

    REST_FRAMEWORK = {
        'UNAUTHENTICATED_USER': None,
        'UNAUTHENTICATED_TOKEN': None,
        "DEFAULT_AUTHENTICATION_CLASSES": [
            "web.utils.TestAuthentication",
        ],
        "DEFAULT_PERMISSION_CLASSES": [
            "web.utils.TestPermission",
        ],
    }
    
  • urls.py

    from django.conf.urls import url, include
    from web.views import TestView
    
    urlpatterns = [
        url(r'^test/', TestView.as_view()),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    
    class TestView(APIView):
    
        def get(self, request, *args, **kwargs):
            # self.dispatch
            print(request.user)
            print(request.auth)
            return Response('GET请求,响应内容')
    
        def post(self, request, *args, **kwargs):
            return Response('POST请求,响应内容')
    
        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')
    

一. 中坚流程

url.py

from django.conf.urls import url, include
from web.views.s1_api import TestView

urlpatterns = [
    url(r'^test/', TestView.as_view()),
]

views.py

from rest_framework.views import APIView
from rest_framework.response import Response


class TestView(APIView):
    def dispatch(self, request, *args, **kwargs):
        """
        请求到来之后,都要执行dispatch方法,dispatch方法根据请求方式不同触发 get/post/put等方法

        注意:APIView中的dispatch方法有好多好多的功能
        """
        return super().dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

上述是rest
framework框架基本流程,重要的效果是在APIView的dispatch中触发。

        二、前后端分离

3. 用户访问次数/频率限制

  • urls.py

    from django.conf.urls import url, include
    from web.views import TestView
    
    urlpatterns = [
        url(r'^test/', TestView.as_view()),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import time
    from rest_framework.views import APIView
    from rest_framework.response import Response
    
    from rest_framework import exceptions
    from rest_framework.throttling import BaseThrottle
    from rest_framework.settings import api_settings
    
    # 保存访问记录
    RECORD = {
        '用户IP': [12312139, 12312135, 12312133, ]
    }
    
    class TestThrottle(BaseThrottle):
        ctime = time.time

        def get_ident(self, request):
            """
            根据用户IP和代理IP,当做请求者的唯一IP
            Identify the machine making the request by parsing HTTP_X_FORWARDED_FOR
            if present and number of proxies is > 0. If not use all of
            HTTP_X_FORWARDED_FOR if it is available, if not use REMOTE_ADDR.
            """
            xff = request.META.get('HTTP_X_FORWARDED_FOR')
            remote_addr = request.META.get('REMOTE_ADDR')
            num_proxies = api_settings.NUM_PROXIES

            if num_proxies is not None:
                if num_proxies == 0 or xff is None:
                    return remote_addr
                addrs = xff.split(',')
                client_addr = addrs[-min(num_proxies, len(addrs))]
                return client_addr.strip()

            return ''.join(xff.split()) if xff else remote_addr

        def allow_request(self, request, view):
            """
            是否仍然在允许范围内
            Return `True` if the request should be allowed, `False` otherwise.
            :param request: 
            :param view: 
            :return: True,表示可以通过;False表示已超过限制,不允许访问
            """
            # 获取用户唯一标识(如:IP)

            # 允许一分钟访问10次
            num_request = 10
            time_request = 60

            now = self.ctime()
            ident = self.get_ident(request)
            self.ident = ident
            if ident not in RECORD:
                RECORD[ident] = [now, ]
                return True
            history = RECORD[ident]
            while history and history[-1] <= now - time_request:
                history.pop()
            if len(history) < num_request:
                history.insert(0, now)
                return True

        def wait(self):
            """
            多少秒后可以允许继续访问
            Optionally, return a recommended number of seconds to wait before
            the next request.
            """
            last_time = RECORD[self.ident][0]
            now = self.ctime()
            return int(60 + last_time - now)


    class TestView(APIView):
        throttle_classes = [TestThrottle, ]

        def get(self, request, *args, **kwargs):
            # self.dispatch
            print(request.user)
            print(request.auth)
            return Response('GET请求,响应内容')

        def post(self, request, *args, **kwargs):
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

        def throttled(self, request, wait):
            """
            访问次数被限制时,定制错误信息
            """

            class Throttled(exceptions.Throttled):
                default_detail = '请求被限制.'
                extra_detail_singular = '请 {wait} 秒之后再重试.'
                extra_detail_plural = '请 {wait} 秒之后再重试.'

            raise Throttled(wait)

b. 基于用户IP突显访问频率(利于Django缓存)

  • settings.py

    REST_FRAMEWORK = {
        'DEFAULT_THROTTLE_RATES': {
            'test_scope': '10/m',
        },
    }
    
  • urls.py

    from django.conf.urls import url, include
    from web.views import TestView
    
    urlpatterns = [
        url(r'^test/', TestView.as_view()),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    
    from rest_framework import exceptions
    from rest_framework.throttling import SimpleRateThrottle
    
    class TestThrottle(SimpleRateThrottle):

        # 配置文件定义的显示频率的Key
        scope = "test_scope"

        def get_cache_key(self, request, view):
            """
            Should return a unique cache-key which can be used for throttling.
            Must be overridden.

            May return `None` if the request should not be throttled.
            """
            if not request.user:
                ident = self.get_ident(request)
            else:
                ident = request.user

            return self.cache_format % {
                'scope': self.scope,
                'ident': ident
            }


    class TestView(APIView):
        throttle_classes = [TestThrottle, ]

        def get(self, request, *args, **kwargs):
            # self.dispatch
            print(request.user)
            print(request.auth)
            return Response('GET请求,响应内容')

        def post(self, request, *args, **kwargs):
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

        def throttled(self, request, wait):
            """
            访问次数被限制时,定制错误信息
            """

            class Throttled(exceptions.Throttled):
                default_detail = '请求被限制.'
                extra_detail_singular = '请 {wait} 秒之后再重试.'
                extra_detail_plural = '请 {wait} 秒之后再重试.'

            raise Throttled(wait)

c. view中限制请求频率

  • settings.py

    REST_FRAMEWORK = {
        'DEFAULT_THROTTLE_RATES': {
            'xxxxxx': '10/m',
        },
    }
    
  • urls.py

    from django.conf.urls import url, include
    from web.views import TestView
    
    urlpatterns = [
        url(r'^test/', TestView.as_view()),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    
    from rest_framework import exceptions
    from rest_framework.throttling import ScopedRateThrottle
    
    # 继承 ScopedRateThrottle
    class TestThrottle(ScopedRateThrottle):

        def get_cache_key(self, request, view):
            """
            Should return a unique cache-key which can be used for throttling.
            Must be overridden.

            May return `None` if the request should not be throttled.
            """
            if not request.user:
                ident = self.get_ident(request)
            else:
                ident = request.user

            return self.cache_format % {
                'scope': self.scope,
                'ident': ident
            }


    class TestView(APIView):
        throttle_classes = [TestThrottle, ]

        # 在settings中获取 xxxxxx 对应的频率限制值
        throttle_scope = "xxxxxx"

        def get(self, request, *args, **kwargs):
            # self.dispatch
            print(request.user)
            print(request.auth)
            return Response('GET请求,响应内容')

        def post(self, request, *args, **kwargs):
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

        def throttled(self, request, wait):
            """
            访问次数被限制时,定制错误信息
            """

            class Throttled(exceptions.Throttled):
                default_detail = '请求被限制.'
                extra_detail_singular = '请 {wait} 秒之后再重试.'
                extra_detail_plural = '请 {wait} 秒之后再重试.'

            raise Throttled(wait)

d. 匿名时用IP限制+登录时用Token限制

  • settings.py

    REST_FRAMEWORK = {
        'UNAUTHENTICATED_USER': None,
        'UNAUTHENTICATED_TOKEN': None,
        'DEFAULT_THROTTLE_RATES': {
            'luffy_anon': '10/m',
            'luffy_user': '20/m',
        },
    }
    
  • urls.py

    from django.conf.urls import url, include
    from web.views.s3_throttling import TestView
    
    urlpatterns = [
        url(r'^test/', TestView.as_view()),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    
    from rest_framework.throttling import SimpleRateThrottle
    
    class LuffyAnonRateThrottle(SimpleRateThrottle):
        """
        匿名用户,根据IP进行限制
        """
        scope = "luffy_anon"

        def get_cache_key(self, request, view):
            # 用户已登录,则跳过 匿名频率限制
            if request.user:
                return None

            return self.cache_format % {
                'scope': self.scope,
                'ident': self.get_ident(request)
            }


    class LuffyUserRateThrottle(SimpleRateThrottle):
        """
        登录用户,根据用户token限制
        """
        scope = "luffy_user"

        def get_ident(self, request):
            """
            认证成功时:request.user是用户对象;request.auth是token对象
            :param request: 
            :return: 
            """
            # return request.auth.token
            return "user_token"

        def get_cache_key(self, request, view):
            """
            获取缓存key
            :param request: 
            :param view: 
            :return: 
            """
            # 未登录用户,则跳过 Token限制
            if not request.user:
                return None

            return self.cache_format % {
                'scope': self.scope,
                'ident': self.get_ident(request)
            }


    class TestView(APIView):
        throttle_classes = [LuffyUserRateThrottle, LuffyAnonRateThrottle, ]

        def get(self, request, *args, **kwargs):
            # self.dispatch
            print(request.user)
            print(request.auth)
            return Response('GET请求,响应内容')

        def post(self, request, *args, **kwargs):
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

e. 全局使用

  • settings

    REST_FRAMEWORK = {
        'DEFAULT_THROTTLE_CLASSES': [
            'api.utils.throttles.throttles.LuffyAnonRateThrottle',
            'api.utils.throttles.throttles.LuffyUserRateThrottle',
        ],
        'DEFAULT_THROTTLE_RATES': {
            'anon': '10/day',
            'user': '10/day',
            'luffy_anon': '10/m',
            'luffy_user': '20/m',
        },
    }
    

2.  证实和授权

a. 用户url传入的token认证

美高梅开户网址 3美高梅开户网址 4

from django.conf.urls import url, include
from web.viewsimport TestView

urlpatterns = [
    url(r'^test/', TestView.as_view()),
]

urls.py

美高梅开户网址 5美高梅开户网址 6

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.authentication import BaseAuthentication
from rest_framework.request import Request
from rest_framework import exceptions

token_list = [
    'sfsfss123kuf3j123',
    'asijnfowerkkf9812',
]


class TestAuthentication(BaseAuthentication):
    def authenticate(self, request):
        """
        用户认证,如果验证成功后返回元组: (用户,用户Token)
        :param request: 
        :return: 
            None,表示跳过该验证;
                如果跳过了所有认证,默认用户和Token和使用配置文件进行设置
                self._authenticator = None
                if api_settings.UNAUTHENTICATED_USER:
                    self.user = api_settings.UNAUTHENTICATED_USER()
                else:
                    self.user = None

                if api_settings.UNAUTHENTICATED_TOKEN:
                    self.auth = api_settings.UNAUTHENTICATED_TOKEN()
                else:
                    self.auth = None
            (user,token)表示验证通过并设置用户名和Token;
            AuthenticationFailed异常
        """
        val = request.query_params.get('token')
        if val not in token_list:
            raise exceptions.AuthenticationFailed("用户认证失败")

        return ('登录用户', '用户token')

    def authenticate_header(self, request):
        """
        Return a string to be used as the value of the `WWW-Authenticate`
        header in a `401 Unauthenticated` response, or `None` if the
        authentication scheme should return `403 Permission Denied` responses.
        """
        # 验证失败时,返回的响应头WWW-Authenticate对应的值
        pass


class TestView(APIView):
    authentication_classes = [TestAuthentication, ]
    permission_classes = []

    def get(self, request, *args, **kwargs):
        print(request.user)
        print(request.auth)
        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

b. 请求头认证

美高梅开户网址 7美高梅开户网址 8

from django.conf.urls import url, include
from web.viewsimport TestView

urlpatterns = [
    url(r'^test/', TestView.as_view()),
]

urls.py

美高梅开户网址 9美高梅开户网址 10

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.authentication import BaseAuthentication
from rest_framework.request import Request
from rest_framework import exceptions

token_list = [
    'sfsfss123kuf3j123',
    'asijnfowerkkf9812',
]


class TestAuthentication(BaseAuthentication):
    def authenticate(self, request):
        """
        用户认证,如果验证成功后返回元组: (用户,用户Token)
        :param request: 
        :return: 
            None,表示跳过该验证;
                如果跳过了所有认证,默认用户和Token和使用配置文件进行设置
                self._authenticator = None
                if api_settings.UNAUTHENTICATED_USER:
                    self.user = api_settings.UNAUTHENTICATED_USER()
                else:
                    self.user = None

                if api_settings.UNAUTHENTICATED_TOKEN:
                    self.auth = api_settings.UNAUTHENTICATED_TOKEN()
                else:
                    self.auth = None
            (user,token)表示验证通过并设置用户名和Token;
            AuthenticationFailed异常
        """
        import base64
        auth = request.META.get('HTTP_AUTHORIZATION', b'')
        if auth:
            auth = auth.encode('utf-8')
        auth = auth.split()
        if not auth or auth[0].lower() != b'basic':
            raise exceptions.AuthenticationFailed('验证失败')
        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('验证失败')
        username, part, password = base64.b64decode(auth[1]).decode('utf-8').partition(':')
        if username == 'alex' and password == '123':
            return ('登录用户', '用户token')
        else:
            raise exceptions.AuthenticationFailed('用户名或密码错误')

    def authenticate_header(self, request):
        """
        Return a string to be used as the value of the `WWW-Authenticate`
        header in a `401 Unauthenticated` response, or `None` if the
        authentication scheme should return `403 Permission Denied` responses.
        """
        return 'Basic realm=api'


class TestView(APIView):
    authentication_classes = [TestAuthentication, ]
    permission_classes = []

    def get(self, request, *args, **kwargs):
        print(request.user)
        print(request.auth)
        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

c. 多个验证规则

美高梅开户网址 11美高梅开户网址 12

from django.conf.urls import url, include
from web.views.s2_auth import TestView

urlpatterns = [
    url(r'^test/', TestView.as_view()),
]

urls.py

美高梅开户网址 13美高梅开户网址 14

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.authentication import BaseAuthentication
from rest_framework.request import Request
from rest_framework import exceptions

token_list = [
    'sfsfss123kuf3j123',
    'asijnfowerkkf9812',
]


class Test1Authentication(BaseAuthentication):
    def authenticate(self, request):
        """
        用户认证,如果验证成功后返回元组: (用户,用户Token)
        :param request: 
        :return: 
            None,表示跳过该验证;
                如果跳过了所有认证,默认用户和Token和使用配置文件进行设置
                self._authenticator = None
                if api_settings.UNAUTHENTICATED_USER:
                    self.user = api_settings.UNAUTHENTICATED_USER() # 默认值为:匿名用户
                else:
                    self.user = None

                if api_settings.UNAUTHENTICATED_TOKEN:
                    self.auth = api_settings.UNAUTHENTICATED_TOKEN()# 默认值为:None
                else:
                    self.auth = None
            (user,token)表示验证通过并设置用户名和Token;
            AuthenticationFailed异常
        """
        import base64
        auth = request.META.get('HTTP_AUTHORIZATION', b'')
        if auth:
            auth = auth.encode('utf-8')
        else:
            return None
        print(auth,'xxxx')
        auth = auth.split()
        if not auth or auth[0].lower() != b'basic':
            raise exceptions.AuthenticationFailed('验证失败')
        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('验证失败')
        username, part, password = base64.b64decode(auth[1]).decode('utf-8').partition(':')
        if username == 'alex' and password == '123':
            return ('登录用户', '用户token')
        else:
            raise exceptions.AuthenticationFailed('用户名或密码错误')

    def authenticate_header(self, request):
        """
        Return a string to be used as the value of the `WWW-Authenticate`
        header in a `401 Unauthenticated` response, or `None` if the
        authentication scheme should return `403 Permission Denied` responses.
        """
        # return 'Basic realm=api'
        pass

class Test2Authentication(BaseAuthentication):
    def authenticate(self, request):
        """
        用户认证,如果验证成功后返回元组: (用户,用户Token)
        :param request: 
        :return: 
            None,表示跳过该验证;
                如果跳过了所有认证,默认用户和Token和使用配置文件进行设置
                self._authenticator = None
                if api_settings.UNAUTHENTICATED_USER:
                    self.user = api_settings.UNAUTHENTICATED_USER() # 默认值为:匿名用户
                else:
                    self.user = None

                if api_settings.UNAUTHENTICATED_TOKEN:
                    self.auth = api_settings.UNAUTHENTICATED_TOKEN()# 默认值为:None
                else:
                    self.auth = None
            (user,token)表示验证通过并设置用户名和Token;
            AuthenticationFailed异常
        """
        val = request.query_params.get('token')
        if val not in token_list:
            raise exceptions.AuthenticationFailed("用户认证失败")

        return ('登录用户', '用户token')

    def authenticate_header(self, request):
        """
        Return a string to be used as the value of the `WWW-Authenticate`
        header in a `401 Unauthenticated` response, or `None` if the
        authentication scheme should return `403 Permission Denied` responses.
        """
        pass


class TestView(APIView):
    authentication_classes = [Test1Authentication, Test2Authentication]
    permission_classes = []

    def get(self, request, *args, **kwargs):
        print(request.user)
        print(request.auth)
        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

d. 认证和权力

美高梅开户网址 15美高梅开户网址 16

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'^test/', TestView.as_view()),
]

urls.py

美高梅开户网址 17美高梅开户网址 18

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.authentication import BaseAuthentication
from rest_framework.permissions import BasePermission

from rest_framework.request import Request
from rest_framework import exceptions

token_list = [
    'sfsfss123kuf3j123',
    'asijnfowerkkf9812',
]


class TestAuthentication(BaseAuthentication):
    def authenticate(self, request):
        """
        用户认证,如果验证成功后返回元组: (用户,用户Token)
        :param request: 
        :return: 
            None,表示跳过该验证;
                如果跳过了所有认证,默认用户和Token和使用配置文件进行设置
                self._authenticator = None
                if api_settings.UNAUTHENTICATED_USER:
                    self.user = api_settings.UNAUTHENTICATED_USER() # 默认值为:匿名用户
                else:
                    self.user = None

                if api_settings.UNAUTHENTICATED_TOKEN:
                    self.auth = api_settings.UNAUTHENTICATED_TOKEN()# 默认值为:None
                else:
                    self.auth = None
            (user,token)表示验证通过并设置用户名和Token;
            AuthenticationFailed异常
        """
        val = request.query_params.get('token')
        if val not in token_list:
            raise exceptions.AuthenticationFailed("用户认证失败")

        return ('登录用户', '用户token')

    def authenticate_header(self, request):
        """
        Return a string to be used as the value of the `WWW-Authenticate`
        header in a `401 Unauthenticated` response, or `None` if the
        authentication scheme should return `403 Permission Denied` responses.
        """
        pass


class TestPermission(BasePermission):
    message = "权限验证失败"

    def has_permission(self, request, view):
        """
        判断是否有权限访问当前请求
        Return `True` if permission is granted, `False` otherwise.
        :param request: 
        :param view: 
        :return: True有权限;False无权限
        """
        if request.user == "管理员":
            return True

    # GenericAPIView中get_object时调用
    def has_object_permission(self, request, view, obj):
        """
        视图继承GenericAPIView,并在其中使用get_object时获取对象时,触发单独对象权限验证
        Return `True` if permission is granted, `False` otherwise.
        :param request: 
        :param view: 
        :param obj: 
        :return: True有权限;False无权限
        """
        if request.user == "管理员":
            return True


class TestView(APIView):
    # 认证的动作是由request.user触发
    authentication_classes = [TestAuthentication, ]

    # 权限
    # 循环执行所有的权限
    permission_classes = [TestPermission, ]

    def get(self, request, *args, **kwargs):
        # self.dispatch
        print(request.user)
        print(request.auth)
        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

e. 全局使用

上述操作中均是对独立视图举办特殊配备,如若想要对全局举办配置,则必要再配置文件中写入即可。

美高梅开户网址 19美高梅开户网址 20

REST_FRAMEWORK = {
    'UNAUTHENTICATED_USER': None,
    'UNAUTHENTICATED_TOKEN': None,
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "web.utils.TestAuthentication",
    ],
    "DEFAULT_PERMISSION_CLASSES": [
        "web.utils.TestPermission",
    ],
}

settings.py

美高梅开户网址 21美高梅开户网址 22

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'^test/', TestView.as_view()),
]

urls.py

美高梅开户网址 23美高梅开户网址 24

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response

class TestView(APIView):

    def get(self, request, *args, **kwargs):
        # self.dispatch
        print(request.user)
        print(request.auth)
        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

二、restful

4.版本

a. 基于url的get传参方式

如:/users?version=v1

  • settings

    REST_FRAMEWORK = {
        'DEFAULT_VERSION': 'v1',            # 默认版本
        'ALLOWED_VERSIONS': ['v1', 'v2'],   # 允许的版本
        'VERSION_PARAM': 'version'          # URL中获取值的key
    }
    
  • urls.py

    from django.conf.urls import url, include
    from web.views import TestView
    
    urlpatterns = [
        url(r'^test/', TestView.as_view(),name='test'),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.versioning import QueryParameterVersioning
    
    class TestView(APIView):
        versioning_class = QueryParameterVersioning

        def get(self, request, *args, **kwargs):

            # 获取版本
            print(request.version)
            # 获取版本管理的类
            print(request.versioning_scheme)

            # 反向生成URL
            reverse_url = request.versioning_scheme.reverse('test', request=request)
            print(reverse_url)

            return Response('GET请求,响应内容')

        def post(self, request, *args, **kwargs):
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

b. 基于url的正则格局

如:/v1/users/

  • settings.py

    REST_FRAMEWORK = {
        'DEFAULT_VERSION': 'v1',            # 默认版本
        'ALLOWED_VERSIONS': ['v1', 'v2'],   # 允许的版本
        'VERSION_PARAM': 'version'          # URL中获取值的key
    }
    
  • urls.py

    from django.conf.urls import url, include
    from web.views import TestView
    
    urlpatterns = [
        url(r'^(?P<version>[v1|v2]+)/test/', TestView.as_view(), name='test'),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.versioning import URLPathVersioning
    
    class TestView(APIView):
        versioning_class = URLPathVersioning

        def get(self, request, *args, **kwargs):
            # 获取版本
            print(request.version)
            # 获取版本管理的类
            print(request.versioning_scheme)

            # 反向生成URL
            reverse_url = request.versioning_scheme.reverse('test', request=request)
            print(reverse_url)

            return Response('GET请求,响应内容')

        def post(self, request, *args, **kwargs):
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

c. 基于 accept 请求头方式

如:Accept: application/json; version=1.0

  • settings.py

    REST_FRAMEWORK = {
        'DEFAULT_VERSION': 'v1',            # 默认版本
        'ALLOWED_VERSIONS': ['v1', 'v2'],   # 允许的版本
        'VERSION_PARAM': 'version'          # URL中获取值的key
    }
    
  • urls.py

    from django.conf.urls import url, include
    from web.views import TestView
    
    urlpatterns = [
        url(r'^test/', TestView.as_view(), name='test'),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.versioning import AcceptHeaderVersioning
    
    class TestView(APIView):
        versioning_class = AcceptHeaderVersioning

        def get(self, request, *args, **kwargs):
            # 获取版本 HTTP_ACCEPT头
            print(request.version)
            # 获取版本管理的类
            print(request.versioning_scheme)
            # 反向生成URL
            reverse_url = request.versioning_scheme.reverse('test', request=request)
            print(reverse_url)

            return Response('GET请求,响应内容')

        def post(self, request, *args, **kwargs):
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

d. 基于主机名方法

如:v1.example.com

  • settings.py

    ALLOWED_HOSTS = [‘*’]
    REST_FRAMEWORK = {
    ‘DEFAULT_VERSION’: ‘v1’, # 暗中同意版本
    ‘ALLOWED_VERSIONS’: [‘v1’, ‘v2’], # 允许的版本
    ‘VERSION_PARAM’: ‘version’ # U奥德赛L中拿走值的key
    }

  • urls.py

    from django.conf.urls import url, include
    from web.views import TestView
    
    urlpatterns = [
        url(r'^test/', TestView.as_view(), name='test'),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.versioning import HostNameVersioning
    
    class TestView(APIView):
        versioning_class = HostNameVersioning

        def get(self, request, *args, **kwargs):
            # 获取版本
            print(request.version)
            # 获取版本管理的类
            print(request.versioning_scheme)
            # 反向生成URL
            reverse_url = request.versioning_scheme.reverse('test', request=request)
            print(reverse_url)

            return Response('GET请求,响应内容')

        def post(self, request, *args, **kwargs):
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

e. 基于django路由系统的namespace

如:example.com/v1/users/

    REST_FRAMEWORK = {
        'DEFAULT_VERSION': 'v1',  # 默认版本
        'ALLOWED_VERSIONS': ['v1', 'v2'],  # 允许的版本
        'VERSION_PARAM': 'version'  # URL中获取值的key
    }
  • urls.py

    from django.conf.urls import url, include
    from web.views import TestView
    
    urlpatterns = [
        url(r'^v1/', ([
                          url(r'test/', TestView.as_view(), name='test'),
                      ], None, 'v1')),
        url(r'^v2/', ([
                          url(r'test/', TestView.as_view(), name='test'),
                      ], None, 'v2')),
    
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.versioning import NamespaceVersioning
    
    class TestView(APIView):
        versioning_class = NamespaceVersioning

        def get(self, request, *args, **kwargs):
            # 获取版本
            print(request.version)
            # 获取版本管理的类
            print(request.versioning_scheme)
            # 反向生成URL
            reverse_url = request.versioning_scheme.reverse('test', request=request)
            print(reverse_url)

            return Response('GET请求,响应内容')

        def post(self, request, *args, **kwargs):
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

f. 全局使用

  • settings.py

    REST_FRAMEWORK = {
        'DEFAULT_VERSIONING_CLASS':"rest_framework.versioning.URLPathVersioning",
        'DEFAULT_VERSION': 'v1',
        'ALLOWED_VERSIONS': ['v1', 'v2'],
        'VERSION_PARAM': 'version' 
    }
    

三. 用户访问次数/频率限制

a. 基于用户IP限制访问频率

美高梅开户网址 25美高梅开户网址 26

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'^test/', TestView.as_view()),
]

urls.py

美高梅开户网址 27美高梅开户网址 28

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import time
from rest_framework.views import APIView
from rest_framework.response import Response

from rest_framework import exceptions
from rest_framework.throttling import BaseThrottle
from rest_framework.settings import api_settings

# 保存访问记录
RECORD = {
    '用户IP': [12312139, 12312135, 12312133, ]
}


class TestThrottle(BaseThrottle):
    ctime = time.time

    def get_ident(self, request):
        """
        根据用户IP和代理IP,当做请求者的唯一IP
        Identify the machine making the request by parsing HTTP_X_FORWARDED_FOR
        if present and number of proxies is > 0. If not use all of
        HTTP_X_FORWARDED_FOR if it is available, if not use REMOTE_ADDR.
        """
        xff = request.META.get('HTTP_X_FORWARDED_FOR')
        remote_addr = request.META.get('REMOTE_ADDR')
        num_proxies = api_settings.NUM_PROXIES

        if num_proxies is not None:
            if num_proxies == 0 or xff is None:
                return remote_addr
            addrs = xff.split(',')
            client_addr = addrs[-min(num_proxies, len(addrs))]
            return client_addr.strip()

        return ''.join(xff.split()) if xff else remote_addr

    def allow_request(self, request, view):
        """
        是否仍然在允许范围内
        Return `True` if the request should be allowed, `False` otherwise.
        :param request: 
        :param view: 
        :return: True,表示可以通过;False表示已超过限制,不允许访问
        """
        # 获取用户唯一标识(如:IP)

        # 允许一分钟访问10次
        num_request = 10
        time_request = 60

        now = self.ctime()
        ident = self.get_ident(request)
        self.ident = ident
        if ident not in RECORD:
            RECORD[ident] = [now, ]
            return True
        history = RECORD[ident]
        while history and history[-1] <= now - time_request:
            history.pop()
        if len(history) < num_request:
            history.insert(0, now)
            return True

    def wait(self):
        """
        多少秒后可以允许继续访问
        Optionally, return a recommended number of seconds to wait before
        the next request.
        """
        last_time = RECORD[self.ident][0]
        now = self.ctime()
        return int(60 + last_time - now)


class TestView(APIView):
    throttle_classes = [TestThrottle, ]

    def get(self, request, *args, **kwargs):
        # self.dispatch
        print(request.user)
        print(request.auth)
        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

    def throttled(self, request, wait):
        """
        访问次数被限制时,定制错误信息
        """

        class Throttled(exceptions.Throttled):
            default_detail = '请求被限制.'
            extra_detail_singular = '请 {wait} 秒之后再重试.'
            extra_detail_plural = '请 {wait} 秒之后再重试.'

        raise Throttled(wait)

views.py

b. 基于用户IP呈现访问频率(利于Django缓存)

美高梅开户网址 29美高梅开户网址 30

REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_RATES': {
        'test_scope': '10/m',
    },
}

settings.py

美高梅开户网址 31美高梅开户网址 32

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'^test/', TestView.as_view()),
]

urls.py

美高梅开户网址 33美高梅开户网址 34

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response

from rest_framework import exceptions
from rest_framework.throttling import SimpleRateThrottle


class TestThrottle(SimpleRateThrottle):

    # 配置文件定义的显示频率的Key
    scope = "test_scope"

    def get_cache_key(self, request, view):
        """
        Should return a unique cache-key which can be used for throttling.
        Must be overridden.

        May return `None` if the request should not be throttled.
        """
        if not request.user:
            ident = self.get_ident(request)
        else:
            ident = request.user

        return self.cache_format % {
            'scope': self.scope,
            'ident': ident
        }


class TestView(APIView):
    throttle_classes = [TestThrottle, ]

    def get(self, request, *args, **kwargs):
        # self.dispatch
        print(request.user)
        print(request.auth)
        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

    def throttled(self, request, wait):
        """
        访问次数被限制时,定制错误信息
        """

        class Throttled(exceptions.Throttled):
            default_detail = '请求被限制.'
            extra_detail_singular = '请 {wait} 秒之后再重试.'
            extra_detail_plural = '请 {wait} 秒之后再重试.'

        raise Throttled(wait)

views.py

c. view中限制请求频率

美高梅开户网址 35美高梅开户网址 36

REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_RATES': {
        'xxxxxx': '10/m',
    },
}

settings.py

美高梅开户网址 37美高梅开户网址 38

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'^test/', TestView.as_view()),
]

urls.py

美高梅开户网址 39美高梅开户网址 40

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response

from rest_framework import exceptions
from rest_framework.throttling import ScopedRateThrottle


# 继承 ScopedRateThrottle
class TestThrottle(ScopedRateThrottle):

    def get_cache_key(self, request, view):
        """
        Should return a unique cache-key which can be used for throttling.
        Must be overridden.

        May return `None` if the request should not be throttled.
        """
        if not request.user:
            ident = self.get_ident(request)
        else:
            ident = request.user

        return self.cache_format % {
            'scope': self.scope,
            'ident': ident
        }


class TestView(APIView):
    throttle_classes = [TestThrottle, ]

    # 在settings中获取 xxxxxx 对应的频率限制值
    throttle_scope = "xxxxxx"

    def get(self, request, *args, **kwargs):
        # self.dispatch
        print(request.user)
        print(request.auth)
        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

    def throttled(self, request, wait):
        """
        访问次数被限制时,定制错误信息
        """

        class Throttled(exceptions.Throttled):
            default_detail = '请求被限制.'
            extra_detail_singular = '请 {wait} 秒之后再重试.'
            extra_detail_plural = '请 {wait} 秒之后再重试.'

        raise Throttled(wait)

views.py

d. 匿名时用IP限制+登录时用Token限制

美高梅开户网址 41美高梅开户网址 42

REST_FRAMEWORK = {
    'UNAUTHENTICATED_USER': None,
    'UNAUTHENTICATED_TOKEN': None,
    'DEFAULT_THROTTLE_RATES': {
        'luffy_anon': '10/m',
        'luffy_user': '20/m',
    },
}

settings.py

美高梅开户网址 43美高梅开户网址 44

from django.conf.urls import url, include
from web.views.s3_throttling import TestView

urlpatterns = [
    url(r'^test/', TestView.as_view()),
]

urls.py

美高梅开户网址 45美高梅开户网址 46

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response

from rest_framework.throttling import SimpleRateThrottle


class LuffyAnonRateThrottle(SimpleRateThrottle):
    """
    匿名用户,根据IP进行限制
    """
    scope = "luffy_anon"

    def get_cache_key(self, request, view):
        # 用户已登录,则跳过 匿名频率限制
        if request.user:
            return None

        return self.cache_format % {
            'scope': self.scope,
            'ident': self.get_ident(request)
        }


class LuffyUserRateThrottle(SimpleRateThrottle):
    """
    登录用户,根据用户token限制
    """
    scope = "luffy_user"

    def get_ident(self, request):
        """
        认证成功时:request.user是用户对象;request.auth是token对象
        :param request: 
        :return: 
        """
        # return request.auth.token
        return "user_token"

    def get_cache_key(self, request, view):
        """
        获取缓存key
        :param request: 
        :param view: 
        :return: 
        """
        # 未登录用户,则跳过 Token限制
        if not request.user:
            return None

        return self.cache_format % {
            'scope': self.scope,
            'ident': self.get_ident(request)
        }


class TestView(APIView):
    throttle_classes = [LuffyUserRateThrottle, LuffyAnonRateThrottle, ]

    def get(self, request, *args, **kwargs):
        # self.dispatch
        print(request.user)
        print(request.auth)
        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

e. 全局使用

美高梅开户网址 47美高梅开户网址 48

REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_CLASSES': [
        'api.utils.throttles.throttles.LuffyAnonRateThrottle',
        'api.utils.throttles.throttles.LuffyUserRateThrottle',
    ],
    'DEFAULT_THROTTLE_RATES': {
        'anon': '10/day',
        'user': '10/day',
        'luffy_anon': '10/m',
        'luffy_user': '20/m',
    },
}

settings

    a、–字面意思:表征状态转移

5.解析器(parser)

依照请求头 content-type 采取相应的解析器就请求体内容开展处理。

a. 仅处理请求头content-type为application/json的请求体

  • urls.py

    from django.conf.urls import url, include
    from web.views.s5_parser import TestView
    
    urlpatterns = [
        url(r'test/', TestView.as_view(), name='test'),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.request import Request
    from rest_framework.parsers import JSONParser
    
    class TestView(APIView):
        parser_classes = [JSONParser, ]

        def post(self, request, *args, **kwargs):
            print(request.content_type)

            # 获取请求的值,并使用对应的JSONParser进行处理
            print(request.data)

            # application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值
            print(request.POST)
            print(request.FILES)

            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

b. 仅处理请求头content-type为application/x-www-form-urlencoded
的请求体

  • urls.py

    from django.conf.urls import url, include
    from web.views import TestView
    
    urlpatterns = [
        url(r'test/', TestView.as_view(), name='test'),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.request import Request
    from rest_framework.parsers import FormParser
    
    class TestView(APIView):
        parser_classes = [FormParser, ]

        def post(self, request, *args, **kwargs):
            print(request.content_type)

            # 获取请求的值,并使用对应的JSONParser进行处理
            print(request.data)

            # application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值
            print(request.POST)
            print(request.FILES)

            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

c. 仅处理请求头content-type为multipart/form-data的请求体

  • urls.py

    from django.conf.urls import url, include
    from web.views import TestView
    
    urlpatterns = [
        url(r'test/', TestView.as_view(), name='test'),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.request import Request
    from rest_framework.parsers import MultiPartParser
    
    class TestView(APIView):
        parser_classes = [MultiPartParser, ]

        def post(self, request, *args, **kwargs):
            print(request.content_type)

            # 获取请求的值,并使用对应的JSONParser进行处理
            print(request.data)
            # application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值
            print(request.POST)
            print(request.FILES)
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')
  • upload.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form action="http://127.0.0.1:8000/test/" method="post" enctype="multipart/form-data">
        <input type="text" name="user" />
        <input type="file" name="img">
    
        <input type="submit" value="提交">
    
    </form>
    </body>
    </html>
    

d. 仅上传文件

  • urls.py

    from django.conf.urls import url, include
    from web.views import TestView
    
    urlpatterns = [
        url(r'test/(?P<filename>[^/]+)', TestView.as_view(), name='test'),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.request import Request
    from rest_framework.parsers import FileUploadParser
    
    class TestView(APIView):
        parser_classes = [FileUploadParser, ]

        def post(self, request, filename, *args, **kwargs):
            print(filename)
            print(request.content_type)

            # 获取请求的值,并使用对应的JSONParser进行处理
            print(request.data)
            # application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值
            print(request.POST)
            print(request.FILES)
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')
  • upload.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form action="http://127.0.0.1:8000/test/f1.numbers" method="post" enctype="multipart/form-data">
        <input type="text" name="user" />
        <input type="file" name="img">
    
        <input type="submit" value="提交">
    
    </form>
    </body>
    </html>
    

e. 同时四个Parser

当同时利用多个parser时,rest
framework会依据请求头content-type自动进行比对,并选取对应parser

  • urls.py

        from django.conf.urls import url, include
        from web.views import TestView
    
        urlpatterns = [
            url(r'test/', TestView.as_view(), name='test'),
        ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.request import Request
    from rest_framework.parsers import JSONParser, FormParser, MultiPartParser
    
    class TestView(APIView):
        parser_classes = [JSONParser, FormParser, MultiPartParser, ]

        def post(self, request, *args, **kwargs):
            print(request.content_type)

            # 获取请求的值,并使用对应的JSONParser进行处理
            print(request.data)
            # application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值
            print(request.POST)
            print(request.FILES)
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

f. 全局使用

  • settings.py

    REST_FRAMEWORK = {
        'DEFAULT_PARSER_CLASSES':[
            'rest_framework.parsers.JSONParser'
            'rest_framework.parsers.FormParser'
            'rest_framework.parsers.MultiPartParser'
        ]
    
    }
    
  • urls.py

    from django.conf.urls import url, include
    from web.views import TestView
    
    urlpatterns = [
        url(r'test/', TestView.as_view(), name='test'),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    
    class TestView(APIView):
        def post(self, request, *args, **kwargs):
            print(request.content_type)

            # 获取请求的值,并使用对应的JSONParser进行处理
            print(request.data)
            # application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值
            print(request.POST)
            print(request.FILES)
            return Response('POST请求,响应内容')

        def put(self, request, *args, **kwargs):
            return Response('PUT请求,响应内容')

专注:个别特殊的值能够因此Django的request对象 request._request
来进展获取

4. 版本

a. 基于url的get传参情势

如:/users?version=v1

美高梅开户网址 49美高梅开户网址 50

REST_FRAMEWORK = {
    'DEFAULT_VERSION': 'v1',            # 默认版本
    'ALLOWED_VERSIONS': ['v1', 'v2'],   # 允许的版本
    'VERSION_PARAM': 'version'          # URL中获取值的key
}

settings.py

美高梅开户网址 51美高梅开户网址 52

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'^test/', TestView.as_view(),name='test'),
]

urls.py

美高梅开户网址 53美高梅开户网址 54

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.versioning import QueryParameterVersioning


class TestView(APIView):
    versioning_class = QueryParameterVersioning

    def get(self, request, *args, **kwargs):

        # 获取版本
        print(request.version)
        # 获取版本管理的类
        print(request.versioning_scheme)

        # 反向生成URL
        reverse_url = request.versioning_scheme.reverse('test', request=request)
        print(reverse_url)

        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

b. 基于url的正则格局

如:/v1/users/

美高梅开户网址 55美高梅开户网址 56

REST_FRAMEWORK = {
    'DEFAULT_VERSION': 'v1',            # 默认版本
    'ALLOWED_VERSIONS': ['v1', 'v2'],   # 允许的版本
    'VERSION_PARAM': 'version'          # URL中获取值的key
}

settings.py

美高梅开户网址 57美高梅开户网址 58

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'^(?P<version>[v1|v2]+)/test/', TestView.as_view(), name='test'),
]

urls.py

美高梅开户网址 59美高梅开户网址 60

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.versioning import URLPathVersioning


class TestView(APIView):
    versioning_class = URLPathVersioning

    def get(self, request, *args, **kwargs):
        # 获取版本
        print(request.version)
        # 获取版本管理的类
        print(request.versioning_scheme)

        # 反向生成URL
        reverse_url = request.versioning_scheme.reverse('test', request=request)
        print(reverse_url)

        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

c. 基于 accept 请求头情势

如:Accept:
application/json; version=1.0

美高梅开户网址 61美高梅开户网址 62

REST_FRAMEWORK = {
    'DEFAULT_VERSION': 'v1',            # 默认版本
    'ALLOWED_VERSIONS': ['v1', 'v2'],   # 允许的版本
    'VERSION_PARAM': 'version'          # URL中获取值的key
}

settings.py

美高梅开户网址 63美高梅开户网址 64

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'^test/', TestView.as_view(), name='test'),
]

urls.py

美高梅开户网址 65美高梅开户网址 66

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.versioning import AcceptHeaderVersioning


class TestView(APIView):
    versioning_class = AcceptHeaderVersioning

    def get(self, request, *args, **kwargs):
        # 获取版本 HTTP_ACCEPT头
        print(request.version)
        # 获取版本管理的类
        print(request.versioning_scheme)
        # 反向生成URL
        reverse_url = request.versioning_scheme.reverse('test', request=request)
        print(reverse_url)

        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

d. 基于主机名方法

如:v1.example.com

美高梅开户网址 67美高梅开户网址 68

ALLOWED_HOSTS = ['*']
REST_FRAMEWORK = {
    'DEFAULT_VERSION': 'v1',  # 默认版本
    'ALLOWED_VERSIONS': ['v1', 'v2'],  # 允许的版本
    'VERSION_PARAM': 'version'  # URL中获取值的key
}

settings.py

美高梅开户网址 69美高梅开户网址 70

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'^test/', TestView.as_view(), name='test'),
]

urls.py

美高梅开户网址 71美高梅开户网址 72

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.versioning import HostNameVersioning


class TestView(APIView):
    versioning_class = HostNameVersioning

    def get(self, request, *args, **kwargs):
        # 获取版本
        print(request.version)
        # 获取版本管理的类
        print(request.versioning_scheme)
        # 反向生成URL
        reverse_url = request.versioning_scheme.reverse('test', request=request)
        print(reverse_url)

        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

e. 基于django路由系统的namespace

如:example.com/v1/users/

美高梅开户网址 73美高梅开户网址 74

REST_FRAMEWORK = {
    'DEFAULT_VERSION': 'v1',  # 默认版本
    'ALLOWED_VERSIONS': ['v1', 'v2'],  # 允许的版本
    'VERSION_PARAM': 'version'  # URL中获取值的key
}

settings.py

美高梅开户网址 75美高梅开户网址 76

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'^v1/', ([
                      url(r'test/', TestView.as_view(), name='test'),
                  ], None, 'v1')),
    url(r'^v2/', ([
                      url(r'test/', TestView.as_view(), name='test'),
                  ], None, 'v2')),

]

urls.py

美高梅开户网址 77美高梅开户网址 78

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.versioning import NamespaceVersioning


class TestView(APIView):
    versioning_class = NamespaceVersioning

    def get(self, request, *args, **kwargs):
        # 获取版本
        print(request.version)
        # 获取版本管理的类
        print(request.versioning_scheme)
        # 反向生成URL
        reverse_url = request.versioning_scheme.reverse('test', request=request)
        print(reverse_url)

        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

f. 全局使用

美高梅开户网址 79美高梅开户网址 80

REST_FRAMEWORK = {
    'DEFAULT_VERSIONING_CLASS':"rest_framework.versioning.URLPathVersioning",
    'DEFAULT_VERSION': 'v1',
    'ALLOWED_VERSIONS': ['v1', 'v2'],
    'VERSION_PARAM': 'version' 
}

settings.py

    b、面向能源编制程序,对网络上的随机东西都实属财富

6.序列化

连串化用于对用户请求数据进行求证和多少开始展览体系化。

a. 自定义字段

  • urls.py

    from django.conf.urls import url, include
    from web.views.s6_serializers import TestView
    
    urlpatterns = [
        url(r'test/', TestView.as_view(), name='test'),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework import serializers
    from .. import models
    
    class PasswordValidator(object):
        def __init__(self, base):
            self.base = base

        def __call__(self, value):
            if value != self.base:
                message = 'This field must be %s.' % self.base
                raise serializers.ValidationError(message)

        def set_context(self, serializer_field):
            """
            This hook is called by the serializer instance,
            prior to the validation call being made.
            """
            # 执行验证之前调用,serializer_fields是当前字段对象
            pass


    class UserSerializer(serializers.Serializer):
        ut_title = serializers.CharField(source='ut.title')
        user = serializers.CharField(min_length=6)
        pwd = serializers.CharField(error_messages={'required': '密码不能为空'}, validators=[PasswordValidator('666')])


    class TestView(APIView):
        def get(self, request, *args, **kwargs):

            # 序列化,将数据库查询字段序列化为字典
            data_list = models.UserInfo.objects.all()
            ser = UserSerializer(instance=data_list, many=True)
            # 或
            # obj = models.UserInfo.objects.all().first()
            # ser = UserSerializer(instance=obj, many=False)
            return Response(ser.data)

        def post(self, request, *args, **kwargs):
            # 验证,对请求发来的数据进行验证
            ser = UserSerializer(data=request.data)
            if ser.is_valid():
                print(ser.validated_data)
            else:
                print(ser.errors)

            return Response('POST请求,响应内容')

b. 基于Model自动生成字段

  • urls.py

    from django.conf.urls import url, include
    from web.views.s6_serializers import TestView
    
    urlpatterns = [
        url(r'test/', TestView.as_view(), name='test'),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework import serializers
    from .. import models
    
    class PasswordValidator(object):
        def __init__(self, base):
            self.base = str(base)

        def __call__(self, value):
            if value != self.base:
                message = 'This field must be %s.' % self.base
                raise serializers.ValidationError(message)

        def set_context(self, serializer_field):
            """
            This hook is called by the serializer instance,
            prior to the validation call being made.
            """
            # 执行验证之前调用,serializer_fields是当前字段对象
            pass

    class ModelUserSerializer(serializers.ModelSerializer):

        user = serializers.CharField(max_length=32)

        class Meta:
            model = models.UserInfo
            fields = "__all__"
            # fields = ['user', 'pwd', 'ut']
            depth = 2
            extra_kwargs = {'user': {'min_length': 6}, 'pwd': {'validators': [PasswordValidator(666), ]}}
            # read_only_fields = ['user']


    class TestView(APIView):
        def get(self, request, *args, **kwargs):

            # 序列化,将数据库查询字段序列化为字典
            data_list = models.UserInfo.objects.all()
            ser = ModelUserSerializer(instance=data_list, many=True)
            # 或
            # obj = models.UserInfo.objects.all().first()
            # ser = UserSerializer(instance=obj, many=False)
            return Response(ser.data)

        def post(self, request, *args, **kwargs):
            # 验证,对请求发来的数据进行验证
            print(request.data)
            ser = ModelUserSerializer(data=request.data)
            if ser.is_valid():
                print(ser.validated_data)
            else:
                print(ser.errors)

            return Response('POST请求,响应内容')

c. 生成URL

  • urls.py

    from django.conf.urls import url, include
    from web.views.s6_serializers import TestView
    
    urlpatterns = [
        url(r'test/', TestView.as_view(), name='test'),
        url(r'detail/(?P<pk>\d+)/', TestView.as_view(), name='detail'),
    ]
    
  • views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework import serializers
    from .. import models
    
    class PasswordValidator(object):
        def __init__(self, base):
            self.base = str(base)

        def __call__(self, value):
            if value != self.base:
                message = 'This field must be %s.' % self.base
                raise serializers.ValidationError(message)

        def set_context(self, serializer_field):
            """
            This hook is called by the serializer instance,
            prior to the validation call being made.
            """
            # 执行验证之前调用,serializer_fields是当前字段对象
            pass


    class ModelUserSerializer(serializers.ModelSerializer):
        ut = serializers.HyperlinkedIdentityField(view_name='detail')
        class Meta:
            model = models.UserInfo
            fields = "__all__"

            extra_kwargs = {
                'user': {'min_length': 6},
                'pwd': {'validators': [PasswordValidator(666),]},
            }



    class TestView(APIView):
        def get(self, request, *args, **kwargs):

            # 序列化,将数据库查询字段序列化为字典
            data_list = models.UserInfo.objects.all()
            ser = ModelUserSerializer(instance=data_list, many=True, context={'request': request})
            # 或
            # obj = models.UserInfo.objects.all().first()
            # ser = UserSerializer(instance=obj, many=False)
            return Response(ser.data)

        def post(self, request, *args, **kwargs):
            # 验证,对请求发来的数据进行验证
            print(request.data)
            ser = ModelUserSerializer(data=request.data)
            if ser.is_valid():
                print(ser.validated_data)
            else:
                print(ser.errors)

            return Response('POST请求,响应内容')

5. 解析器(parser) 

依据请求头 content-type 选择相应的解析器就请求体内容举行处理。

a. 仅处理请求头content-type为application/json的请求体

美高梅开户网址 81美高梅开户网址 82

from django.conf.urls import url, include
from web.views.s5_parser import TestView

urlpatterns = [
    url(r'test/', TestView.as_view(), name='test'),
]

urls.py

美高梅开户网址 83美高梅开户网址 84

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.request import Request
from rest_framework.parsers import JSONParser


class TestView(APIView):
    parser_classes = [JSONParser, ]

    def post(self, request, *args, **kwargs):
        print(request.content_type)

        # 获取请求的值,并使用对应的JSONParser进行处理
        print(request.data)

        # application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值
        print(request.POST)
        print(request.FILES)

        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

b. 仅处理请求头content-type为application/x-www-form-urlencoded 的请求体

美高梅开户网址 85美高梅开户网址 86

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'test/', TestView.as_view(), name='test'),
]

urls.py

美高梅开户网址 87美高梅开户网址 88

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.request import Request
from rest_framework.parsers import FormParser


class TestView(APIView):
    parser_classes = [FormParser, ]

    def post(self, request, *args, **kwargs):
        print(request.content_type)

        # 获取请求的值,并使用对应的JSONParser进行处理
        print(request.data)

        # application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值
        print(request.POST)
        print(request.FILES)

        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

c. 仅处理请求头content-type为multipart/form-data的请求体

美高梅开户网址 89美高梅开户网址 90

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'test/', TestView.as_view(), name='test'),
]

urls.py

美高梅开户网址 91美高梅开户网址 92

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.request import Request
from rest_framework.parsers import MultiPartParser


class TestView(APIView):
    parser_classes = [MultiPartParser, ]

    def post(self, request, *args, **kwargs):
        print(request.content_type)

        # 获取请求的值,并使用对应的JSONParser进行处理
        print(request.data)
        # application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值
        print(request.POST)
        print(request.FILES)
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

美高梅开户网址 93美高梅开户网址 94

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="http://127.0.0.1:8000/test/" method="post" enctype="multipart/form-data">
    <input type="text" name="user" />
    <input type="file" name="img">

    <input type="submit" value="提交">

</form>
</body>
</html>

upload.html

d. 仅上传文件

美高梅开户网址 95美高梅开户网址 96

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'test/(?P<filename>[^/]+)', TestView.as_view(), name='test'),
]

urls.py

美高梅开户网址 97美高梅开户网址 98

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.request import Request
from rest_framework.parsers import FileUploadParser


class TestView(APIView):
    parser_classes = [FileUploadParser, ]

    def post(self, request, filename, *args, **kwargs):
        print(filename)
        print(request.content_type)

        # 获取请求的值,并使用对应的JSONParser进行处理
        print(request.data)
        # application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值
        print(request.POST)
        print(request.FILES)
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

美高梅开户网址 99美高梅开户网址 100

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="http://127.0.0.1:8000/test/f1.numbers" method="post" enctype="multipart/form-data">
    <input type="text" name="user" />
    <input type="file" name="img">

    <input type="submit" value="提交">

</form>
</body>
</html>

upload.html

美高梅开户网址 ,e. 同时四个Parser

当同时利用四个parser时,rest
framework会依据请求头content-type自动举办比对,并选择对应parser

美高梅开户网址 101美高梅开户网址 102

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'test/', TestView.as_view(), name='test'),
]

urls.py

美高梅开户网址 103美高梅开户网址 104

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.request import Request
from rest_framework.parsers import JSONParser, FormParser, MultiPartParser


class TestView(APIView):
    parser_classes = [JSONParser, FormParser, MultiPartParser, ]

    def post(self, request, *args, **kwargs):
        print(request.content_type)

        # 获取请求的值,并使用对应的JSONParser进行处理
        print(request.data)
        # application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值
        print(request.POST)
        print(request.FILES)
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

f. 全局使用

美高梅开户网址 105美高梅开户网址 106

REST_FRAMEWORK = {
    'DEFAULT_PARSER_CLASSES':[
        'rest_framework.parsers.JSONParser'
        'rest_framework.parsers.FormParser'
        'rest_framework.parsers.MultiPartParser'
    ]

}

settings.py

美高梅开户网址 107美高梅开户网址 108

from django.conf.urls import url, include
from web.views import TestView

urlpatterns = [
    url(r'test/', TestView.as_view(), name='test'),
]

urls.py

美高梅开户网址 109美高梅开户网址 110

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response


class TestView(APIView):
    def post(self, request, *args, **kwargs):
        print(request.content_type)

        # 获取请求的值,并使用对应的JSONParser进行处理
        print(request.data)
        # application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值
        print(request.POST)
        print(request.FILES)
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

views.py

在意:个别特殊的值能够由此Django的request对象
request._request 来进行获取

         如:-

6. 序列化

系列化用于对用户请求数据举行验证和多少实行系列化。

a. 自定义字段

美高梅开户网址 111美高梅开户网址 112

from django.conf.urls import url, include
from web.views.s6_serializers import TestView

urlpatterns = [
    url(r'test/', TestView.as_view(), name='test'),
]

urls.py

美高梅开户网址 113美高梅开户网址 114

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers
from .. import models


class PasswordValidator(object):
    def __init__(self, base):
        self.base = base

    def __call__(self, value):
        if value != self.base:
            message = 'This field must be %s.' % self.base
            raise serializers.ValidationError(message)

    def set_context(self, serializer_field):
        """
        This hook is called by the serializer instance,
        prior to the validation call being made.
        """
        # 执行验证之前调用,serializer_fields是当前字段对象
        pass


class UserSerializer(serializers.Serializer):
    ut_title = serializers.CharField(source='ut.title')
    user = serializers.CharField(min_length=6)
    pwd = serializers.CharField(error_messages={'required': '密码不能为空'}, validators=[PasswordValidator('666')])


class TestView(APIView):
    def get(self, request, *args, **kwargs):

        # 序列化,将数据库查询字段序列化为字典
        data_list = models.UserInfo.objects.all()
        ser = UserSerializer(instance=data_list, many=True)
        # 或
        # obj = models.UserInfo.objects.all().first()
        # ser = UserSerializer(instance=obj, many=False)
        return Response(ser.data)

    def post(self, request, *args, **kwargs):
        # 验证,对请求发来的数据进行验证
        ser = UserSerializer(data=request.data)
        if ser.is_valid():
            print(ser.validated_data)
        else:
            print(ser.errors)

        return Response('POST请求,响应内容')

views.py

b. 基于Model自动生成字段

美高梅开户网址 115美高梅开户网址 116

from django.conf.urls import url, include
from web.views.s6_serializers import TestView

urlpatterns = [
    url(r'test/', TestView.as_view(), name='test'),
]

urls.py

美高梅开户网址 117美高梅开户网址 118

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers
from .. import models


class PasswordValidator(object):
    def __init__(self, base):
        self.base = str(base)

    def __call__(self, value):
        if value != self.base:
            message = 'This field must be %s.' % self.base
            raise serializers.ValidationError(message)

    def set_context(self, serializer_field):
        """
        This hook is called by the serializer instance,
        prior to the validation call being made.
        """
        # 执行验证之前调用,serializer_fields是当前字段对象
        pass

class ModelUserSerializer(serializers.ModelSerializer):

    user = serializers.CharField(max_length=32)

    class Meta:
        model = models.UserInfo
        fields = "__all__"
        # fields = ['user', 'pwd', 'ut']
        depth = 2
        extra_kwargs = {'user': {'min_length': 6}, 'pwd': {'validators': [PasswordValidator(666), ]}}
        # read_only_fields = ['user']


class TestView(APIView):
    def get(self, request, *args, **kwargs):

        # 序列化,将数据库查询字段序列化为字典
        data_list = models.UserInfo.objects.all()
        ser = ModelUserSerializer(instance=data_list, many=True)
        # 或
        # obj = models.UserInfo.objects.all().first()
        # ser = UserSerializer(instance=obj, many=False)
        return Response(ser.data)

    def post(self, request, *args, **kwargs):
        # 验证,对请求发来的数据进行验证
        print(request.data)
        ser = ModelUserSerializer(data=request.data)
        if ser.is_valid():
            print(ser.validated_data)
        else:
            print(ser.errors)

        return Response('POST请求,响应内容')

views.py

c. 生成URL

美高梅开户网址 119美高梅开户网址 120

from django.conf.urls import url, include
from web.views.s6_serializers import TestView

urlpatterns = [
    url(r'test/', TestView.as_view(), name='test'),
    url(r'detail/(?P<pk>\d+)/', TestView.as_view(), name='detail'),
]

urls.py

美高梅开户网址 121美高梅开户网址 122

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers
from .. import models


class PasswordValidator(object):
    def __init__(self, base):
        self.base = str(base)

    def __call__(self, value):
        if value != self.base:
            message = 'This field must be %s.' % self.base
            raise serializers.ValidationError(message)

    def set_context(self, serializer_field):
        """
        This hook is called by the serializer instance,
        prior to the validation call being made.
        """
        # 执行验证之前调用,serializer_fields是当前字段对象
        pass


class ModelUserSerializer(serializers.ModelSerializer):
    ut = serializers.HyperlinkedIdentityField(view_name='detail')
    class Meta:
        model = models.UserInfo
        fields = "__all__"

        extra_kwargs = {
            'user': {'min_length': 6},
            'pwd': {'validators': [PasswordValidator(666),]},
        }



class TestView(APIView):
    def get(self, request, *args, **kwargs):

        # 序列化,将数据库查询字段序列化为字典
        data_list = models.UserInfo.objects.all()
        ser = ModelUserSerializer(instance=data_list, many=True, context={'request': request})
        # 或
        # obj = models.UserInfo.objects.all().first()
        # ser = UserSerializer(instance=obj, many=False)
        return Response(ser.data)

    def post(self, request, *args, **kwargs):
        # 验证,对请求发来的数据进行验证
        print(request.data)
        ser = ModelUserSerializer(data=request.data)
        if ser.is_valid():
            print(ser.validated_data)
        else:
            print(ser.errors)

        return Response('POST请求,响应内容')

views.py

d. 自动生成UKugaL

美高梅开户网址 123美高梅开户网址 124

from django.conf.urls import url, include
from web.views.s6_serializers import TestView

urlpatterns = [
    url(r'test/', TestView.as_view(), name='test'),
    url(r'detail/(?P<pk>\d+)/', TestView.as_view(), name='xxxx'),
]

urls.py

美高梅开户网址 125美高梅开户网址 126

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers
from .. import models


class PasswordValidator(object):
    def __init__(self, base):
        self.base = str(base)

    def __call__(self, value):
        if value != self.base:
            message = 'This field must be %s.' % self.base
            raise serializers.ValidationError(message)

    def set_context(self, serializer_field):
        """
        This hook is called by the serializer instance,
        prior to the validation call being made.
        """
        # 执行验证之前调用,serializer_fields是当前字段对象
        pass


class ModelUserSerializer(serializers.HyperlinkedModelSerializer):
    ll = serializers.HyperlinkedIdentityField(view_name='xxxx')
    tt = serializers.CharField(required=False)

    class Meta:
        model = models.UserInfo
        fields = "__all__"
        list_serializer_class = serializers.ListSerializer

        extra_kwargs = {
            'user': {'min_length': 6},
            'pwd': {'validators': [PasswordValidator(666), ]},
            'url': {'view_name': 'xxxx'},
            'ut': {'view_name': 'xxxx'},
        }


class TestView(APIView):
    def get(self, request, *args, **kwargs):
        # # 序列化,将数据库查询字段序列化为字典
        data_list = models.UserInfo.objects.all()
        ser = ModelUserSerializer(instance=data_list, many=True, context={'request': request})
        # # 如果Many=True
        # # 或
        # # obj = models.UserInfo.objects.all().first()
        # # ser = UserSerializer(instance=obj, many=False)
        return Response(ser.data)

    def post(self, request, *args, **kwargs):
        # 验证,对请求发来的数据进行验证
        print(request.data)
        ser = ModelUserSerializer(data=request.data)
        if ser.is_valid():
            print(ser.validated_data)
        else:
            print(ser.errors)

        return Response('POST请求,响应内容')

views.py

                –

7. 分页

a. 依据页码举办分页

美高梅开户网址 127美高梅开户网址 128

from django.conf.urls import url, include
from rest_framework import routers
from web.views import s9_pagination

urlpatterns = [
    url(r'^test/', s9_pagination.UserViewSet.as_view()),
]

urs.py

美高梅开户网址 129美高梅开户网址 130

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework import serializers
from .. import models

from rest_framework.pagination import PageNumberPagination


class StandardResultsSetPagination(PageNumberPagination):
    # 默认每页显示的数据条数
    page_size = 1
    # 获取URL参数中设置的每页显示数据条数
    page_size_query_param = 'page_size'

    # 获取URL参数中传入的页码key
    page_query_param = 'page'

    # 最大支持的每页显示的数据条数
    max_page_size = 1


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserInfo
        fields = "__all__"


class UserViewSet(APIView):
    def get(self, request, *args, **kwargs):
        user_list = models.UserInfo.objects.all().order_by('-id')

        # 实例化分页对象,获取数据库中的分页数据
        paginator = StandardResultsSetPagination()
        page_user_list = paginator.paginate_queryset(user_list, self.request, view=self)

        # 序列化对象
        serializer = UserSerializer(page_user_list, many=True)

        # 生成分页和数据
        response = paginator.get_paginated_response(serializer.data)
        return response

views.py

b. 地点和个数进行分页

美高梅开户网址 131美高梅开户网址 132

from django.conf.urls import url, include
from web.views import s9_pagination

urlpatterns = [
    url(r'^test/', s9_pagination.UserViewSet.as_view()),
]

urls.py

美高梅开户网址 133美高梅开户网址 134

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework import serializers
from .. import models

from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination


class StandardResultsSetPagination(LimitOffsetPagination):
    # 默认每页显示的数据条数
    default_limit = 10
    # URL中传入的显示数据条数的参数
    limit_query_param = 'limit'
    # URL中传入的数据位置的参数
    offset_query_param = 'offset'
    # 最大每页显得条数
    max_limit = None

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserInfo
        fields = "__all__"


class UserViewSet(APIView):
    def get(self, request, *args, **kwargs):
        user_list = models.UserInfo.objects.all().order_by('-id')

        # 实例化分页对象,获取数据库中的分页数据
        paginator = StandardResultsSetPagination()
        page_user_list = paginator.paginate_queryset(user_list, self.request, view=self)

        # 序列化对象
        serializer = UserSerializer(page_user_list, many=True)

        # 生成分页和数据
        response = paginator.get_paginated_response(serializer.data)
        return response

views.py

c. 游标分页

美高梅开户网址 135美高梅开户网址 136

from django.conf.urls import url, include
from web.views import s9_pagination

urlpatterns = [
    url(r'^test/', s9_pagination.UserViewSet.as_view()),
]

urls.py

美高梅开户网址 137美高梅开户网址 138

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework import serializers
from .. import models

from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination


class StandardResultsSetPagination(CursorPagination):
    # URL传入的游标参数
    cursor_query_param = 'cursor'
    # 默认每页显示的数据条数
    page_size = 2
    # URL传入的每页显示条数的参数
    page_size_query_param = 'page_size'
    # 每页显示数据最大条数
    max_page_size = 1000

    # 根据ID从大到小排列
    ordering = "id"



class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserInfo
        fields = "__all__"


class UserViewSet(APIView):
    def get(self, request, *args, **kwargs):
        user_list = models.UserInfo.objects.all().order_by('-id')

        # 实例化分页对象,获取数据库中的分页数据
        paginator = StandardResultsSetPagination()
        page_user_list = paginator.paginate_queryset(user_list, self.request, view=self)

        # 序列化对象
        serializer = UserSerializer(page_user_list, many=True)

        # 生成分页和数据
        response = paginator.get_paginated_response(serializer.data)
        return response

views.py

三、restful规范

八. 路由系统

a. 自定义路由

美高梅开户网址 139美高梅开户网址 140

from django.conf.urls import url, include
from web.views import s11_render

urlpatterns = [
    url(r'^test/$', s11_render.TestView.as_view()),
    url(r'^test\.(?P<format>[a-z0-9]+)$', s11_render.TestView.as_view()),
    url(r'^test/(?P<pk>[^/.]+)/$', s11_render.TestView.as_view()),
    url(r'^test/(?P<pk>[^/.]+)\.(?P<format>[a-z0-9]+)$', s11_render.TestView.as_view())
]

urls.py

美高梅开户网址 141美高梅开户网址 142

from rest_framework.views import APIView
from rest_framework.response import Response
from .. import models


class TestView(APIView):
    def get(self, request, *args, **kwargs):
        print(kwargs)
        print(self.renderer_classes)
        return Response('...')

views.py

b. 半活动路由

美高梅开户网址 143美高梅开户网址 144

from django.conf.urls import url, include
from web.views import s10_generic

urlpatterns = [
    url(r'^test/$', s10_generic.UserViewSet.as_view({'get': 'list', 'post': 'create'})),
    url(r'^test/(?P<pk>\d+)/$', s10_generic.UserViewSet.as_view(
        {'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'})),
]

urls.py

美高梅开户网址 145美高梅开户网址 146

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.viewsets import ModelViewSet
from rest_framework import serializers
from .. import models


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserInfo
        fields = "__all__"


class UserViewSet(ModelViewSet):
    queryset = models.UserInfo.objects.all()
    serializer_class = UserSerializer

views.py

c. 全自动路由

美高梅开户网址 147美高梅开户网址 148

from django.conf.urls import url, include
from rest_framework import routers
from web.views import s10_generic


router = routers.DefaultRouter()
router.register(r'users', s10_generic.UserViewSet)

urlpatterns = [
    url(r'^', include(router.urls)),
]

urls.py

美高梅开户网址 149美高梅开户网址 150

from rest_framework.viewsets import ModelViewSet
from rest_framework import serializers
from .. import models


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserInfo
        fields = "__all__"


class UserViewSet(ModelViewSet):
    queryset = models.UserInfo.objects.all()
    serializer_class = UserSerializer

views.py

     a、 —URL

9. 视图

a. GenericViewSet

美高梅开户网址 151美高梅开户网址 152

from django.conf.urls import url, include
from web.views.s7_viewset import TestView

urlpatterns = [
    url(r'test/', TestView.as_view({'get':'list'}), name='test'),
    url(r'detail/(?P<pk>\d+)/', TestView.as_view({'get':'list'}), name='xxxx'),
]

urls.py

美高梅开户网址 153美高梅开户网址 154

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework import viewsets
from rest_framework.response import Response


class TestView(viewsets.GenericViewSet):
    def list(self, request, *args, **kwargs):
        return Response('...')

    def add(self, request, *args, **kwargs):
        pass

    def delete(self, request, *args, **kwargs):
        pass

    def edit(self, request, *args, **kwargs):
        pass

views.py

b. ModelViewSet(自定义URL)

美高梅开户网址 155美高梅开户网址 156

from django.conf.urls import url, include
from web.views import s10_generic

urlpatterns = [
    url(r'^test/$', s10_generic.UserViewSet.as_view({'get': 'list', 'post': 'create'})),
    url(r'^test/(?P<pk>\d+)/$', s10_generic.UserViewSet.as_view(
        {'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'})),
]

urls.py

美高梅开户网址 157美高梅开户网址 158

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.viewsets import ModelViewSet
from rest_framework import serializers
from .. import models


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserInfo
        fields = "__all__"


class UserViewSet(ModelViewSet):
    queryset = models.UserInfo.objects.all()
    serializer_class = UserSerializer

views.py

c. ModelViewSet(rest framework路由)

美高梅开户网址 159美高梅开户网址 160

from django.conf.urls import url, include
from rest_framework import routers
from app01 import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r'^', include(router.urls)),
]

urls.py

美高梅开户网址 161美高梅开户网址 162

from rest_framework import viewsets
from rest_framework import serializers


class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = models.User
        fields = ('url', 'username', 'email', 'groups')


class GroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = models.Group
        fields = ('url', 'name')

class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer


class GroupViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows groups to be viewed or edited.
    """
    queryset = Group.objects.all()
    serializer_class = GroupSerializer

views.py

     b、 —url名词

10. 渲染器

依照 用户请求U猎豹CS6L 或
用户可承受的类别,筛选出相当的 渲染组件。
用户请求U奥迪Q5L:

  • 用户请求头:

  • Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8

a. json

访问URL:

  •  

美高梅开户网址 163美高梅开户网址 164

from django.conf.urls import url, include
from web.views import s11_render

urlpatterns = [
    url(r'^test/$', s11_render.TestView.as_view()),
    url(r'^test\.(?P<format>[a-z0-9]+)', s11_render.TestView.as_view()),
]

urls.py

美高梅开户网址 165美高梅开户网址 166

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers

from rest_framework.renderers import JSONRenderer

from .. import models


class TestSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserInfo
        fields = "__all__"


class TestView(APIView):
    renderer_classes = [JSONRenderer, ]

    def get(self, request, *args, **kwargs):
        user_list = models.UserInfo.objects.all()
        ser = TestSerializer(instance=user_list, many=True)
        return Response(ser.data)

views.py

b. 表格

访问URL:

  •  

美高梅开户网址 167美高梅开户网址 168

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers

from rest_framework.renderers import AdminRenderer

from .. import models


class TestSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserInfo
        fields = "__all__"


class TestView(APIView):
    renderer_classes = [AdminRenderer, ]

    def get(self, request, *args, **kwargs):
        user_list = models.UserInfo.objects.all()
        ser = TestSerializer(instance=user_list, many=True)
        return Response(ser.data)

views.py

c. Form表单

访问URL:

  •  

美高梅开户网址 169美高梅开户网址 170

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers

from rest_framework.renderers import JSONRenderer
from rest_framework.renderers import AdminRenderer
from rest_framework.renderers import HTMLFormRenderer

from .. import models


class TestSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserInfo
        fields = "__all__"


class TestView(APIView):
    renderer_classes = [HTMLFormRenderer, ]

    def get(self, request, *args, **kwargs):
        user_list = models.UserInfo.objects.all().first()
        ser = TestSerializer(instance=user_list, many=False)
        return Response(ser.data)

views.py

d. 自定义彰显模板

访问URL:

  •  

美高梅开户网址 171美高梅开户网址 172

from django.conf.urls import url, include
from web.views import s11_render

urlpatterns = [
    url(r'^test/$', s11_render.TestView.as_view()),
    url(r'^test\.(?P<format>[a-z0-9]+)', s11_render.TestView.as_view()),
]

urls.py

美高梅开户网址 173美高梅开户网址 174

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers
from rest_framework.renderers import TemplateHTMLRenderer

from .. import models


class TestSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserInfo
        fields = "__all__"


class TestView(APIView):
    renderer_classes = [TemplateHTMLRenderer, ]

    def get(self, request, *args, **kwargs):
        user_list = models.UserInfo.objects.all().first()
        ser = TestSerializer(instance=user_list, many=False)
        return Response(ser.data, template_name='user_detail.html')

views.py

美高梅开户网址 175美高梅开户网址 176

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {{ user }}
    {{ pwd }}
    {{ ut }}
</body>
</html>

userdetail.html

e. 浏览器格式API+JSON

访问URL:

  •  

美高梅开户网址 177美高梅开户网址 178

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers

from rest_framework.renderers import JSONRenderer
from rest_framework.renderers import BrowsableAPIRenderer

from .. import models


class TestSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserInfo
        fields = "__all__"


class CustomBrowsableAPIRenderer(BrowsableAPIRenderer):
    def get_default_renderer(self, view):
        return JSONRenderer()


class TestView(APIView):
    renderer_classes = [CustomBrowsableAPIRenderer, ]

    def get(self, request, *args, **kwargs):
        user_list = models.UserInfo.objects.all().first()
        ser = TestSerializer(instance=user_list, many=False)
        return Response(ser.data, template_name='user_detail.html')

views.py

瞩目:倘使还要多少个设有时,自动依据U本田UR-VL后缀来抉择渲染器。

 

 

 

 

路径,视网络上任何东西都是资源,均使用名词表示(可复数)
  https://api.example.com/v1/zoos
  https://api.example.com/v1/animals
  https://api.example.com/v1/employee

    c、 —status状态码**

200 OK - [GET]:服务器成功返回用户请求的数据,该操作是幂等的(Idempotent)。
201 CREATED - [POST/PUT/PATCH]:用户新建或修改数据成功。
202 Accepted - [*]:表示一个请求已经进入后台排队(异步任务)
204 NO CONTENT - [DELETE]:用户删除数据成功。
400 INVALID REQUEST - [POST/PUT/PATCH]:用户发出的请求有错误,服务器没有进行新建或修改数据的操作,该操作是幂等的。
401 Unauthorized - [*]:表示用户没有权限(令牌、用户名、密码错误)。
403 Forbidden - [*] 表示用户得到授权(与401错误相对),但是访问是被禁止的。
404 NOT FOUND - [*]:用户发出的请求针对的是不存在的记录,服务器没有进行操作,该操作是幂等的。
406 Not Acceptable - [GET]:用户请求的格式不可得(比如用户请求JSON格式,但是只有XML格式)。
410 Gone -[GET]:用户请求的资源被永久删除,且不会再得到的。
422 Unprocesable entity - [POST/PUT/PATCH] 当创建一个对象时,发生一个验证错误。
500 INTERNAL SERVER ERROR - [*]:服务器发生错误,用户将无法判断发出的请求是否成功。

   d、—提交格局

GET      :从服务器取出资源(一项或多项)
POST    :在服务器新建一个资源
PUT      :在服务器更新资源(客户端提供改变后的完整资源)
PATCH  :在服务器更新资源(客户端提供改变的属性)
DELETE :从服务器删除资源

   e、  —错误消息

状态码是4xx时,应返回错误信息,error当做key。
{
    error: "Invalid API key"
}

   f、 —版本**

   g、 —Hypermedia link,RESTful
API最棒达成Hypermedia,即重返结果中提供链接,连向别的API方法,使得用户不查文书档案,也晓得下一步应该做哪些。

   h、—域名**

   j、—过滤,通过在url上传参的款型传递搜索条件**

 
k、 
—再次来到结果,针对分化操作,服务器向用户重返的结果应当符合以下标准**

GET /collection:返回资源对象的列表(数组)  
GET /collection/resource:返回单个资源对象
POST /collection:返回新生成的资源对象
PUT /collection/resource:返回完整的资源对象
PATCH /collection/resource:返回完整的资源对象
DELETE /collection/resource:返回一个空文档

rest_Framework的规范:

   
按梯次:它的method的例外,原来未有设想,原来是url区分,以后通过method来区分,method的两样提交格局各异,紧接着壹般是面向能源的正是把url变成名词,接下就是再次回到值,以前从未有过惦记状态码,以往有怀念状态码。(一般有get,post方法,还有put,delete等方法)

**4、 基于Django Rest Framework框架完结 **

   a、安装:pip3 install djangorestframework -i
–trusted-host=pypi.douban.com

五、什么是RESTful

  • REST与技能非亲非故,代表的是1种软件架构风格,REST是Representational
    State Transfer的简称,汉译为“表征状态转移”
  • REST从能源的角度类审视整个网络,它将遍布在网络中有个别节点的能源通过URAV4L进行标识,客户端应用通过U宝马7系L来获取能源的风味,获得那一个特点致使那么些使用转变状态
  • REST与技能无关,代表的是一种软件架构风格,REST是Representational
    State Transfer的简称,粤语翻译为“表征状态转移”
  • 持有的数量,不过是经过网络获取的或然操作(增加和删除改查)的数目,都以财富,将壹切数据就是财富是REST差别与其他架构风格的最本质属性
  • 对此REST那种面向能源的架构风格,有人建议一种崭新的构造理念,即:面向能源架构(ROA:Resource
    Oriented Architecture)

      a、 django的中间件比rest_framework执行的早**

      b、 认证的功力放到中间件也是足以做的**

     
c、
证实1般做,检查用户是还是不是存在,如若存在request.user/request.auth;不设有request.user/request.auth=None**

      d、证实小计算:**

  ---类:authenticate/authenticate_header

      ---返回值:None,元组(user,auth),异常

      ---配置:

                 ---视图:                      

                                    class IndexView(APIView):
                                           authentication_classes = [MyAuthentication,]

                 ---全局                    

                                    REST_FRAMEWORK = {
                                                'UNAUTHENTICATED_USER': None,
                                                'UNAUTHENTICATED_TOKEN': None,
                                                "DEFAULT_AUTHENTICATION_CLASSES": [
                                                 # "app02.utils.MyAuthentication",
                                                ],
                                               }

六、权限

   
a、
权限才真的的做request.user/request.auth获得它们做是否访问的判定**

    b、权限小总计: **

     ---类:       has_permission/has_object_permission

     ---返回值:True、False、exceptions.PermissionDenied(detail="错误信息")

     ---配置:

               ---视图:                  

                                class IndexView(APIView):
                                       permission_classes = [MyPermission,]

                      ---全局:

                                REST_FRAMEWORK = {
                                "DEFAULT_PERMISSION_CLASSES": [
                                # "app02.utils.MyAuthentication",
                                 ],
                               }

7、限制访问的频率

    限制访问频率的行使:

   a、对匿名用户实行限制,各类用户一分钟允许访问10次

        在此间用唯一标识:self.get_ident() 

   b、限定访问的频率小总括: **

  ---类:       allow_request/wait PS: scope = "wdp_user"

     ---返回值:True、False

     ---配置:

                  ---视图:

                                            class IndexView(APIView):
                                                     throttle_classes=[AnonThrottle,UserThrottle,]
                                                     def get(self,request,*args,**kwargs):
                                                     self.dispatch
                                                     return Response('访问首页')

                   ---全局:

                                          REST_FRAMEWORK = {
                                                      "DEFAULT_THROTTLE_CLASSES":[
                                                   ],
                                                      'DEFAULT_THROTTLE_RATES':{
                                                      'wdp_anon':'5/minute',
                                                      'wdp_user':'10/minute',
                                                   }
                                                   }

、认证、权限、限制访问频率重临结果的可比

    1、表明、权限、限制访问频率那多个再次回到的结果:**

**     a、证实再次来到的二种结果:             a、None             b、元组(user,auth)             c、异常 raise APIException(…)**

**     b、 权限的再次回到值二种结果:**

**            a、True 有权限             b、False 没权限             c、异常**

**     c、 限制访问的频率重回值的二种结果:**

**            a、True 
            b、False**

实例化:
v1 = ["view.xxx.path.Role","view.xxx.path.Group",]    可以循环,循环出来的每一个不能实例化
如果把v1循环弄成每一个对象列表,通过rsplit切割,在通过importlib.import_module拿到每一个路径,在通过getattr把它的类名拿过来,
这个类加括号就是实例化想
for item in v1:
m = importlib.import_module('view.xxx.path')
cls = getattr(m,'Role')
cls()



from view.xxx.path import Role,Group      
v2 = [Group,Role]      这个可以循环每一个实例化
for item in v2:     #循环V2的每一个元素加括号,就是实例化
item()

九、Django rest_Framework框架

     a、  —-为何用Django rest_Framework框架?**

----首先没有Django rest_Framework框架用django也是可以做出来的,只不过它为我们提供一些API常用的功能,比如:(认证,权限,限流,有了这些我们只需要写个类已配置,
它就能当都市图用,还能全局配置,如果自己写还得写中间件,写装饰器来实现,通过Django rest_Framework框架,他已经把规则写好,只需要写类,只需实现方法,返回值就可以)
实现了一部分功能。

  ----设计比较好

            ----单独视图+全局配置 =>Dajngo中间件(importlib/反射)=>动态配置课扩展(短信,邮件,微信等提醒)

     b、Django rest_Framework原理?

             先开始在路由,路由.as_view,

             点击as_view**

**             请求进入,走完以上,才走self.dispatch()**

           
 走
self.dispatch()流程如下地址: 

十、版本

**    a、依据url的例外来来操作,版本控制**

**          先在setting中注册**

**十一、rest framework解析器**

**   
a、伸手的数据开始展览分析:请求体举办分析。表示服务端能够分析的数目格式的体系。**

Content-Type: application/url-encoding.....
            request.body
            request.POST

            Content-Type: application/json.....
            request.body
            request.POST

        客户端:
            Content-Type: application/json
            '{"name":"alex","age":123}'

        服务端接收:
            读取客户端发送的Content-Type的值 application/json

            parser_classes = [JSONParser,]
            media_type_list = ['application/json',]

            如果客户端的Content-Type的值和 application/json 匹配:JSONParser处理数据
            如果客户端的Content-Type的值和 application/x-www-form-urlencoded 匹配:FormParser处理数据


        配置:
            单视图:
            class UsersView(APIView):
                parser_classes = [JSONParser,]

            全局配置:
                REST_FRAMEWORK = {
                    'VERSION_PARAM':'version',
                    'DEFAULT_VERSION':'v1',
                    'ALLOWED_VERSIONS':['v1','v2'],
                    # 'DEFAULT_VERSIONING_CLASS':"rest_framework.versioning.HostNameVersioning"
                    'DEFAULT_VERSIONING_CLASS':"rest_framework.versioning.URLPathVersioning",
                    'DEFAULT_PARSER_CLASSES':[
                        'rest_framework.parsers.JSONParser',
                        'rest_framework.parsers.FormParser',
                    ]
                }

十二、 rest framework序列化+Form

     a、序列化:

               对象 -> 字符串 序列化                字符串 -> 对象 反体系化**

**     b、目的:                消除QuerySet系列化难点**

**十三、怎么要内外端分离**

**    a、因为前端它有自个儿框架,那样它的成效就十二分高**

**   
b、不做上下端分离,公司只要既有客户端,又有app那种景观下你就的写一次**

**十四、django restful框架好处**

  扶助大家写了成都百货上千组件比如:

   
 a、认证:有类,类中的方法
authenticate/authenticate_header,它的再次来到值有None,元组,卓殊。假如重回值为None那就不管,它是匿名用户。**

     b、权限:有类,类中的方法:has_permission**

   
 c、节流:有类,类的方式:allow_request,在这里allow_request来表示限制,它是因而IP来界定,

 它的内部原理是:默认是IP,用户来用IP,这个IP可能代理IP,IP或者是代理IP拿着请求头默认放到大家能够的缓存中                          
去,每一个人的IP为p,后面的那个列表存它的方位时间,每一次请求进来获取它的当前时间根据时间的个数来比较,在                        
比较的过程中看看把不符合时间的个数来进行比较,能访问就继续,不能访问就不要继续。 

   
 d、版本:是url和hostname,他们多个钟每三个都有八个办法一个是帮您拿版本另二个是帮你反向生成url

     e、解析器:用户发过来的请求体数据开始展览操作

     f、连串化:四个职能:体系化,校验

十五、分页

分页的二种景况:

       a、记录当前访问页的数目id

       b、最多显示120页

       c、对页码实行加密

a、基于limit offset做分页

class P1(LimitOffsetPagination):

                max_limit = 3
                default_limit = 2
                limit_query_param = 'limit'
                offset_query_param = 'offset'


            class IndexView(views.APIView):
                def get(self,request,*args,**kwargs):
                    user_list = models.UserInfo.objects.all()
                    p1 = P1()
                    page_user_list = p1.paginate_queryset(queryset=user_list, request=request, view=self)
                    ser = IndexSerializer(instance=page_user_list, many=True)
                    return Response(ser.data) # 不含上一页和下一页
                    # return p1.get_paginated_response(ser.data) # 含上一页和下一页

            class IndexView(views.APIView):
                def get(self,request,*args,**kwargs):
                    ret = BaseResponse()
                    try:
                        user_list = models.UserInfo.objects.all()
                        p1 = P1()
                        page_user_list = p1.paginate_queryset(queryset=user_list,request=request,view=self)
                        ser = IndexSerializer(instance=page_user_list,many=True)
                        ret.data = ser.data
                        ret.next = p1.get_next_link()
                    except Exception as e:
                        ret.code= 1001
                        ret.error = 'xxxx错误'
                    return Response(ret.__dict__)

 b. 基于页码的分页

class P2(PageNumberPagination):
                # 每页显示的数据条数
                max_page_size = 5
                page_size = 2
                page_size_query_param = 'size'

                # 页码
                page_query_param = 'page'

十六、视图

1、APIView

class IndexView(views.APIView):
                    def get(self, request, *args, **kwargs):
                        user_list = models.UserInfo.objects.all()
                        ser = IndexSerializer(instance=user_list,many=True)
                        return Response(ser.data)

2、GenericAPIview(APIView)

3、GenericViewSet(ViewSetMixin,generics.GenericAPIView)

路由修改:
                    urlpatterns = [
                        url(r'^index/$', views.IndexView.as_view({'get':'list','post':'create'})),
                        url(r'^index/(?P<pk>\d+)$', views.IndexView.as_view({'get':'retrieve'})),
                    ]

                视图修改:

                    class IndexView(viewsets.GenericViewSet):

                        def list(self,request,*args,**kwargs):

                             pass # 获取列表信息

                        def retrieve(self, request, *args, **kwargs):
                            pass  # 获取单条数据

                        def create(self,request, *args, **kwargs):
                            pass


                自定义:

                        增
                            POST
                            /users/
                        删
                            DELETE
                            /users/1/
                        改
                            PUT
                            /users/1/

                            patch
                            /users/1/
                        查
                            GET
                            /users/ 
                            GET
                            /users/1/

                        urlpatterns = [

                            url(r'^index/$', views.IndexView.as_view()),
                            url(r'^index/(?P<pk>\d+)$', views.IndexView.as_view()),
                        ]

                        class IndexView(views.APIView):

                            def get(self,request,*args,**kwargs):
                                pk = kwargs.get('pk')
                                if pk:
                                    pass # 获取单条信息
                                else:
                                    pass # 获取列表信息

                            def post(self,request,*args,**kwargs):
                                pass

                            def put(self,request,*args,**kwargs):
                                pass

                            def patch(self,request,*args,**kwargs):
                                pass

                            def delete(self,request,*args,**kwargs):
                                pass

4、ModelViewSet

ModelViewSet(mixins.CreateModelMixin,mixins.RetrieveModelMixin,mixins.UpdateModelMixin,mixins.DestroyModelMixin,mixins.ListModelMixin,GenericViewSet)                

                class IndexView(ModelViewSet):

十七、 渲染器
    看到的页面时怎么样体统的,重返数据。
    renderer_classes = [JSONRenderer,BrowsableAPIRenderer]

十八、跨域

   a、  浏览器的同源策略

              1、  —-对ajax请求进行阻拦

               二、 —-对href属性读不阻碍

       xhr=new XMLHttpRequest

       xhr.open…

       xhr.send(…)

    b、 消除方案:

         —JSONP

                 点击按钮: 

                        动态拉长三个

<script src='http://www.baidu.com/users/'></script>
                        <script>
                            function func(arg){
                                alert(arg)
                            }
                        </script

    c、删除

<script src='http://www.baidu.com/users/'></script>

 

1. 跨域
       浏览器的同源策略
            - 对ajax请求进行阻拦
            - 对href属性都不阻拦

        xhr = new XMLHttpRequest();
        xhr.open...
        xhr.send(...)

       解决方案:
            - JSONP
                点击按钮:
                    动态添加一个
                        <script src='http://www.baidu.com/users/'></script>
                        <script>
                            function func(arg){
                                alert(arg)
                            }
                        </script>
                    删除 
                        <script src='http://www.baidu.com/users/'></script>
                PS: 必问

                客户端浏览器:
                    - jsonp(pythonav)

                Pythondv网站-政府:
                    - jsonp(pythonav)

                Pythonav网站:
                    - {
                            'code':1111,
                            'list':[
                                ...
                                ...
                                ...
                            ]

                        }

            - CORS
                客户端浏览器:
                    - $.ajax(pythonav)

                Pythondv网站-政府:
                    - $.ajax(pythonav)

                Pythonav网站:
                    - {
                            'code':1111,
                            'list':[
                                ...
                                ...
                                ...
                            ]

                        }
                    - 响应头

        a. 简单请求(非常好)

            A网站:
                <input type="button" value="获取用户数据" onclick="getUsers()">

                <script src="jquery-1.12.4.min.js"></script>
                <script>
                    function getUsers() {
                        $.ajax({
                            url: 'http://127.0.0.1:8000/users/',
                            type:'GET',
                            success:function (ret) {
                                console.log(ret)
                            }
                        })
                    }
                </script>

            服务商:
                class UsersView(views.APIView):
                    def get(self,request,*args,**kwargs):

                        ret = {
                            'code':1000,
                            'data':'老男孩'
                        }
                        response = JsonResponse(ret)
                        response['Access-Control-Allow-Origin'] = "*"
                        return response

        b. 复杂请求(性能上的损耗,options预检,真实的请求)
            A网站:
                <input type="button" value="获取用户数据" onclick="getUsers()">

                <script src="jquery-1.12.4.min.js"></script>
                <script>
                    function getUsers() {
                        $.ajax({
                            url: 'http://127.0.0.1:8000/users/',
                            type:'POST',
                            data: {'k1':'v1'},
                            headers:{
                                'h1':'asdfasdfasdf'
                            },
                            success:function (ret) {
                                console.log(ret)
                            }
                        })
                    }
                </script>

            服务商:

                class UsersView(views.APIView):
                    def get(self,request,*args,**kwargs):

                        ret = {
                            'code':1000,
                            'data':'老男孩'
                        }
                        response = JsonResponse(ret)
                        response['Access-Control-Allow-Origin'] = "*"
                        return response

                    def post(self,request,*args,**kwargs):
                        print(request.POST)
                        ret = {
                            'code':1000,
                            'data':'老男孩'
                        }
                        response = JsonResponse(ret)
                        response['Access-Control-Allow-Origin'] = "*"
                        return response

                    def options(self, request, *args, **kwargs):
                        # self.set_header('Access-Control-Allow-Origin', "http://www.xxx.com")
                        # self.set_header('Access-Control-Allow-Headers', "k1,k2")
                        # self.set_header('Access-Control-Allow-Methods', "PUT,DELETE")
                        # self.set_header('Access-Control-Max-Age', 10)

                        response = HttpResponse()
                        response['Access-Control-Allow-Origin'] = '*'
                        response['Access-Control-Allow-Headers'] = 'h1'
                        # response['Access-Control-Allow-Methods'] = 'PUT'
                        return response

    2. Vue+rest示例

        前端:vue
            修改源:
                npm config set registry https://registry.npm.taobao.org 

            创建脚手架:
                vue init webpack Vue项目名称
                # Install vue-router? Yes


            插件:
                axios,发送Ajax请求
                vuex,保存所有组件共用的变量
                vue-cookies,操作cookie


        流程:
            1. 创建脚手架 

            2. 
                # 用于点击查看组件
                <router-link to="/index">首页</router-link>

                # 组件显示的位置
                <router-view/>

            3. 写路由
                    import Vue from 'vue'
                    import Router from 'vue-router'
                    import Index from '@/components/Index'
                    import Login from '@/components/Login'
                    import Course from '@/components/Course'
                    import Micro from '@/components/Micro'
                    import News from '@/components/News'
                    import CourseDetail from '@/components/CourseDetail'
                    import NotFound from '@/components/NotFound'

                    Vue.use(Router)

                    export default new Router({
                      routes: [
                        {
                          path: '/',
                          name: 'index',
                          component: Index
                        },
                        {
                          path: '/index',
                          name: 'index',
                          component: Index
                        },
                        {
                          path: '/course',
                          name: 'course',
                          component: Course
                        },
                        {
                          path: '/course-detail/:id/',
                          name: 'courseDetail',
                          component: CourseDetail
                        },
                        {
                          path: '/micro',
                          name: 'micro',
                          component: Micro
                        },
                        {
                          path: '/news',
                          name: 'news',
                          component: News
                        },
                        {
                          path: '/login',
                          name: 'login',
                          component: Login
                        },
                        {
                          path: '*',
                          component: NotFound
                        }
                      ],
                      mode: 'history'
                    })

            4. 写组件
                <template>

                  <div>
                    <h1>登录页面</h1>
                    <div>
                      <input type="text" v-model="username" placeholder="用户名">
                      <input type="text" v-model="password" placeholder="密码">
                      <a @click="doLogin">提交</a>
                    </div>
                  </div>
                </template>

                <script>

                export default {
                  # 定义局部字段
                  data () {
                    return {
                      username: '',
                      password: ''
                    }
                  },
                  # 加载时执行
                  mounted:function(){
                  },
                  # 定义局部方法
                  methods:{
                    doLogin() {
                      var that = this
                      this.$axios.request({
                        url: 'http://127.0.0.1:8000/login/',
                        method: 'POST',
                        data: {
                          username: this.username,
                          password: this.password
                        },
                        responseType: 'json'
                      }).then(function (response) {
                        console.log(response.data)
                        // 找到全局变量,把用户名和token赋值到其中。
                        that.$store.commit('saveToken',response.data)
                        // 重定向到index
                        that.$router.push('/index')
                      })
                    }
                  }
                }
                </script>

                <!-- Add "scoped" attribute to limit CSS to this component only -->
                <style scoped>

                </style>

            5. ajax请求:axios
                npm install axios

                main.js 
                    import Vue from 'vue'
                    import App from './App'
                    import router from './router'

                    import axios from 'axios'

                    Vue.prototype.$axios = axios

                    Vue.config.productionTip = false
                    ...

                组件使用:
                    this.$axios.request({
                    url: 'http://127.0.0.1:8000/login/',
                    method: 'POST',
                    data: {
                      username: this.username,
                      password: this.password
                    },
                    responseType: 'json'
                  }).then(function (response) {
                    console.log(response.data)

                    that.$router.push('/index')
                  })

                PS:重定向 that.$router.push('/index')

            6. vuex 
                npm install vuex 

                main.js 
                    import Vue from 'vue'
                    import App from './App'
                    import router from './router'
                    import axios from 'axios'

                    import store from './store/store'    # vuex

                    Vue.prototype.$axios = axios

                    Vue.config.productionTip = false

                    /* eslint-disable no-new */
                    new Vue({
                      el: '#app',
                      store,                            # vuex
                      router,
                      components: { App },
                      template: '<App/>'
                    })

                src/store/store.js
                    import Vue from 'vue'
                    import Vuex from 'vuex'
                    import Cookie from 'vue-cookies'

                    Vue.use(Vuex)

                    export default new Vuex.Store({
                      // 组件中通过 this.$store.state.username 调用
                      state: {
                        username: Cookie.get('username'),
                        token: Cookie.get('token')
                      },
                      mutations: {
                        // 组件中通过 this.$store.commit(参数)  调用
                        saveToken: function (state, data) {
                          state.username = data.username
                          state.token = data.token
                          Cookie.set('username', data.username, '20min')
                          Cookie.set('token', data.token, '20min')

                        },
                        clearToken: function (state) {
                          state.username = null
                          state.token = null
                          Cookie.remove('username')
                          Cookie.remove('token')
                        }
                      }
                    })

            7. vue-cookies 
                npm install vue-cookies 


                    Cookie.get('username')

                    Cookie.set('username', data.username, '20min')
                    Cookie.remove('username')


                src/store/store.js
                    import Vue from 'vue'
                    import Vuex from 'vuex'
                    import Cookie from 'vue-cookies'    # vue-cookies

                    Vue.use(Vuex)

                    export default new Vuex.Store({
                      // 组件中通过 this.$store.state.username 调用
                      state: {
                        username: Cookie.get('username'),    # vue-cookies
                        token: Cookie.get('token')            # vue-cookies
                      },
                      mutations: {
                        // 组件中通过 this.$store.commit(参数)  调用
                        saveToken: function (state, data) {
                          state.username = data.username
                          state.token = data.token
                          Cookie.set('username', data.username, '20min')    # vue-cookies
                          Cookie.set('token', data.token, '20min')    

                        },
                        clearToken: function (state) {
                          state.username = null
                          state.token = null
                          Cookie.remove('username')    # vue-cookies
                          Cookie.remove('token')
                        }
                      }
                    })

            8. 路由 
                # 定义路由
                    {
                      path: '/course-detail/:id/',
                      name: 'courseDetail',
                      component: CourseDetail
                    },
                    {
                      path: '/login',
                      name: 'login',
                      component: Login
                    },
                    {
                      path: '*',
                      component: NotFound
                    }


                # router-link参数
                    <router-link :to="{'path':'/course-detail/'+item.id }">{{item.name}}</router-link>
                    <router-link to="/index">首页</router-link>

                # 获取传过来的参数
                    this.$route.params.id
                # 重定向
                    this.$router.push('/index')

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图