面试准备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规范
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):
撸起袖子加油干!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通