面试准备5之rest-framework部分

 

rest-framework部分

1.你理解的Http协议?

    答:1超文本协议,基于tcp协议的应用层协议,端口号80

      本质就是一个socket客户端。请求--》响应--》断开

    2 无连接无状态

      解决无状态有cookie与session策略

   3请求首行 请求头与请求体,请求头与请求体之间用双\r\n分隔

   4 常用的状态码有

    200:成功

    301:临时重定向、302:永久重定向

    403:crsf_token错误

               500:服务器端错误

   4常用的方法有get、post、put、patch、delete、options请求

   5 重要的请求头有cookie、contenttype、useragent、referer(跳转源,防盗链使用)

   请求体:发送post请求后,request.post只能接收urlencoded格式,无法获取ajax发送的请求,而request.body均可以拿到

1. django请求生命周期(包含rest framework框架)  

   wsgi-->中间件--->process_request--->process_匹配到url--->process_view--->执行视图--->process_template_response

2. 中间件是什么?,做过什么?

  答:就是对请求执行前的操作部分,有内置的:csrf、session也有自定义的 

  答:做过:认证、权限、频率、session、静态文件、跨域等操作

 

 

3. csrf原理

请求是带着一个字符串,post请求时需要带着这个字符串

4. restful 10规范

1. 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规范
restful10条规范

 5.restframework组件

      路由--》视图--》版本---》认证--》权限--》频率--》解析器--》序列化--》分页--》-->渲染器

  序列化:

    

1:一对一:字段指定source
2:choices字段:get_字段_display
3:一对多、多对多、反向查询:
  chapter=serializers.SerializerMethodField()
   def get_chapter(self, obj):
        query = obj.course.chapter_set.all()
        return [{'id': row.id, 'name': row.name} for row in query]
class CourseDetailSerializers(serializers.ModelSerializer):
# 一对一字段显示关联表字段----------------------------- title=serializers.CharField(source='course.title')
course_img
=serializers.CharField(source='course.course_img')
# 显示choices字段方法---------(get_字段_display)----------------------------- choices=serializers.CharField(source='course.get_level_display') # 多对多------第一步骤 recommends = serializers.SerializerMethodField() # 一对多(显示章节) chapter=serializers.SerializerMethodField() class Meta: model = CourseDetail fields = ['slogon','why','course','title','course_img','choices','recommends','chapter'] def get_recommends(self,obj):#多对多第二步骤 queryset=obj.recommend_courses.all() return [{'id':row.id,'title':row.title} for row in queryset] def get_chapter(self, obj): query = obj.course.chapter_set.all() return [{'id': row.id, 'name': row.name} for row in query]

 

6.你的写的类都继承过哪些类?

  

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):

 

 





 

posted @ 2018-05-20 10:27  强仔必胜  阅读(547)  评论(0编辑  收藏  举报