11.6路飞学城项目(1)

2018-11-6 20:33:40

终于不挂针啦!这两天一直在下雨!天变冷啦!

身体恢复的不错!

越努力越幸运!永远不要高估自己!!!!!!!

wusir开始讲路飞项目!

 

把重要的东西记一下!

这个是版本信息需要导入的包

 

1,全局配置版本信息

REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES':['rest_framework.renderers.JSONRenderer','rest_framework.renderers.BrowsableAPIRenderer',],

    'DEFAULT_VERSIONING_CLASS':'rest_framework.versioning.URLPathVersioning',
    'ALLOWED_VERSIONS':['v1','v2'], # 允许的版本
    'VERSION_PARAM':'version', # 参数
    'DEFAULT_VERSION':'v1', # 默认版本

}

 2.跨域信息:

参考连接:http://www.cnblogs.com/wupeiqi/articles/7805382.html

这个是中间件,需要在settings中配置一下! 自己的类需要继承MiddlewareMixin

因为中间件都需要,可以参考中间件里面的设置直接引入,这里把类直接copy来了

cors.py

class MiddlewareMixin(object):
    def __init__(self, get_response=None):
        self.get_response = get_response
        super(MiddlewareMixin, self).__init__()

    def __call__(self, request):
        response = None
        if hasattr(self, 'process_request'):
            response = self.process_request(request)
        if not response:
            response = self.get_response(request)
        if hasattr(self, 'process_response'):
            response = self.process_response(request, response)
        return response

class CORSMiddleware(MiddlewareMixin):

    def process_response(self,request,response):
        # 添加响应头

        # 允许你的域名来获取我的数据
        response['Access-Control-Allow-Origin'] = "*"

        # 允许你携带Content-Type请求头
        response['Access-Control-Allow-Headers'] = "Content-Type"

        # 允许你发送DELETE,PUT
        response['Access-Control-Allow-Methods'] = "DELETE,PUT"

        return response

其他的贴上笔记:

s9day104 

课程说明:
    - 路飞
    - flask
    - 爬虫
    - 其他

内容回顾:
    1. django请求生命周期
        
        -> 执行遵循wsgi协议的模块(socket服务端)
        -> 中间件(路由匹配)
        -> 视图函数(业务处理:ORM、模板渲染)
        -> 中间件
        -> wsgi返回
        
    2. 什么wsgi
        web服务网关接口
        实现该协议的模块:
            - wsgiref
            - werkzurg
            - uwsig

    3. 视图
        - FBV
            url - 函数 
        - CBV 
            url - view
    
    4. djang rest framework
        
    
    5. restful 规范(10)
        什么是接口?
            - URL
            - 约束
                # 约束继承(实现)了他的类中必须含有IFoo中的方法
                interface IFoo:
                    def func(self): pass 
                    
                    
                class Foo(IFoo):
                    def func(self): 
                        print(11111)
        

        1. 根据method不同,进行不同操作
            GET/POST/PUT/DELETE/PATCH
        2. 面向资源编程
            http://www.luffycity.com/salary
        
        3. 体现版本
            http://www.luffycity.com/v1/salary
            http://www.luffycity.com/v2/salary
            
            https://v4.bootcss.com/
            https://v3.bootcss.com/
        4. 体现是API
            http://www.luffycity.com/api/v1/salary
            http://www.luffycity.com/api/v2/salary    
            
            http://api.luffycity.com/v1/salary    
            http://api.luffycity.com/v2/salary    
        5. https
            https://www.luffycity.com/api/v1/salary
            https://www.luffycity.com/api/v2/salary    
            
        6. 响应式设置状态码
            200
            300
            400
            500
            return HttpResponse('adfasdf',status=300)
        
        7. 条件 
            https://www.luffycity.com/api/v2/salary?page=1&size=10
        
        8. 返回值
            https://www.luffycity.com/api/v2/salary
            GET: 所有列表
            {
                code: 10000,
                data: [    
                    {'id':1,'title':'高亮'},
                    {'id':1,'title':'龙泰'},
                    {'id':1,'title':'小东北'},
                ]
            }
                
            POST: 返回新增的数据
                {'id':1,'title':'高亮'}
                
            https://www.luffycity.com/api/v2/salary/1/
            GET: 获取单条数据
                    {'id':1,'title':'高亮'}
            PUT:更新
                    {'id':1,'title':'高亮'}
            PATCH: 局部更新
                    {'id':1,'title':'高亮'}
            DELETE:删除
                
        9. 返回错误信息
            {
                code: 100001,
                error: 'xxx错误'
            }
        
        10. Hypermedia API
            ret = {
                code: 1000,
                data:{
                    id:1,
                    name:'小强',
                    depart_id:http://www.luffycity.com/api/v1/depart/8/
                }
            }
    
        建议大家使用restful规范
        
        
    6. django rest framework框架(10)
        rest framework 基本组件: https://www.cnblogs.com/yuanchenqi/articles/8719520.html
        - 权限
        - 认证
        - 访问频率限制
        - 序列化
        - 路由 
        - 视图
            面试题:你的写的类都继承过哪些类?
            class View(object):
            
            class APIView(View):
            
            class GenericAPIView(views.APIView):
            
            class GenericViewSet(ViewSetMixin, generics.GenericAPIView)
            
            class ModelViewSet(mixins.CreateModelMixin,
                   mixins.RetrieveModelMixin,
                   mixins.UpdateModelMixin,
                   mixins.DestroyModelMixin,
                   mixins.ListModelMixin,
                   GenericViewSet):
        - 分页 
        - 解析器
        - 渲染器
        - 版本 

    
今日内容:
    - vue
    - restful api 
    
内容详细:
    1. 渲染器
       规定页面显示的效果(无用)
    2. 版本 
        原理:要了解
        使用:
            1. 添加配置
                REST_FRAMEWORK = {
                    
                    .... 
                    
                    'DEFAULT_VERSIONING_CLASS':'rest_framework.versioning.URLPathVersioning',
                    'ALLOWED_VERSIONS':['v1','v2'], # 允许的版本
                    'VERSION_PARAM':'version', # 参数
                    'DEFAULT_VERSION':'v1', # 默认版本
                    ....
                }

            2. 设置路由 
                
                s9luffycity/urls.py
                    urlpatterns = [
                        #url(r'^admin/', admin.site.urls),
                        url(r'^api/(?P<version>\w+)/', include('api.urls')),
                    ]
                
                api/urls.py 
                    urlpatterns = [
                        url(r'^course/$', course.CourseView.as_view()),
                    ]
            
            3. 获取版本 
                request.version 获取版本  
            
    
    3. vue+rest framework
        vue: 
            - 路由 + 组件 
            - axios发送ajax请求
            - that 
        api:
            - 跨域 

        同源策略和跨域解决方案
        https://www.cnblogs.com/zhen1996/articles/9852658.html
        
        补充:
            - 域名不同
            - 端口不同 
        cors:
            本质设置响应头
                    
                    # 允许你的域名来获取我的数据
                    response['Access-Control-Allow-Origin'] = "*"

                    # 允许你携带Content-Type请求头
                    response['Access-Control-Allow-Headers'] = "Content-Type"

                    # 允许你发送DELETE,PUT
                    response['Access-Control-Allow-Methods'] = "DELETE,PUT"
        
    
作业:
    1.创建两张表
        课程表:
             id    title    
             1    Python全栈
             2    Python周末
             3    Linux
        课程详细表:
            id     name        course_id
            1    Python基础      1
            2    Python进阶      1
            3    Python网络      1
            4    Python并发      1
            5    Python数据库      1
            6    Python前端      1

    2. 两个页面
        - 课程列表
        - 课程详细 
    

 

转载于:https://www.cnblogs.com/zhen1996/p/9917922.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值