• Restframework框架总结及restful规范


    1. django rest framework框架的作用?

    帮助开发者可以快速开发出遵循restful规范的API

    2. django rest framework框架都有哪些组件(10)?

    -版本
    -权限
    -认证
    -节流
    -分页
    -解析器
    -序列化(最重要)
    -路由
    -视图
    -渲染器(把结果渲染到一个模板里)

    3.为什么做前后端分离?

    -分工明确,提高效率
    -对于后端人员,主要为前端提供:API接口
    	以前的你的接口:
    		http://127.0.0.1:8000/index/
    		http://127.0.0.1:8000/users/
    		http://127.0.0.1:8000/add_users/
    		http://127.0.0.1:8000/del_users/
    		http://127.0.0.1:8000/edit_users/
    	restful 规范:
    		http://127.0.0.1:8000/users/ 
    -同样的业务逻辑,能开发多个端口(app,网页端,ios端,微信小程序等),统一一套API接口,能适用于多个端口
    -vue.js等框架编写前端时,会比之前写jQuery更简单快捷
    

    4.什么是跨域?

    -比如你访问百度,在百度js的ajax中携带一个京东的URL发送给你,由于浏览器的同源策略(同源是指域名,协议,端口相同),会进行阻隔,你接收不到页面内容

    5.谈谈你对restful规范的理解(-->所有语言共用)

    1. 使用https代替http(http通过socket收发数据是明文的,https是加密的,安全性高)
    	https://www.luffycity.com/course/detail/web/3
    	http://www.luffycity.com/course/detail/web/3
    	
    2. 在URL中体现自己写的是API
    	https://www.luffycity.com/api/
    	https://api.luffycity.com/		可能会跨域
    
    3. 在URL中体现版本 (旧版本和新版本都能体现出)
    	https://www.luffycity.com/api/v1/users 
    	https://www.luffycity.com/api/v2/users
    	
    4. 名词(面向资源编程)
    	https://www.luffycity.com/api/v1/users 
    	https://www.luffycity.com/api/v1/song
    
    5. 行为 
    	https://www.luffycity.com/api/v1/users
    	method:
    		get,获取
    		post,新建
    		put,更新
    		patch,局部更新
    		delete,删除
    6. 条件 (分页等)
    	https://www.luffycity.com/api/v1/users?page=1
    	https://www.luffycity.com/api/v1/users?page=1&gender=2
    	
    7. 状态码
    	200
    	301
    	302
    	404
    	500
    	推荐使用code:
    		def xx(request):
    			ret = {'code':1000,'data':None}
    			try:
    				...
    			except Exptions as e:
    				ret['status'] = 1001 
    				ret['error'] = 'xxxx错误'
    
    			return JsonResponse(ret)
    			
    8. 错误信息
    	{
    		code:10001,
    		error:'用户名或密码错误'
    	}
    	
    9. 返回结果:
    	GET:
    		https://www.luffycity.com/api/v1/users
    			响应:
    				{
    					code: 1000,
    					data: [
    						{'name':'赵森','age':19},
    						{'name':'赵云','age':16},
    						{'name':'赵云','age':16},
    						{'name':'赵云','age':16},
    						{'name':'赵云','age':16},
    					]
    				}	
    	GET:
    		https://www.luffycity.com/api/v1/users/1/
    			响应:
    				{
    					code:1000,
    					data:{'name':'赵森','age':19},
    				}
    	POST:
    		https://www.luffycity.com/api/v1/users
    			请求体:
    				{'name':'大表哥','age':19}
    			响应(不要):
    				{
    					code:1000,
    					data:{'id':9, 'name':'大表哥','age':19}
    				}
    				
    	PUT/PATCH:
    		https://www.luffycity.com/api/v1/users
    			请求体:
    				{'name':'大表哥','age':19}
    			响应(不要):
    				{
    					code:1000,
    					data:{'id':9, 'name':'大表哥','age':19}
    				}
    	
    	DELETE:
    		...
    10. hyper link 
    	
    	访问:https://www.luffycity.com/api/v1/users
    		{
    			code:1000,
    			data:[
    				{'id':1,'name':'赵森','age':19, 'depart':https://www.luffycity.com/api/v1/depart/1/},
    				{'id':1,'name':'赵森','age':19, 'depart':https://www.luffycity.com/api/v1/depart/1/},
    				{'id':1,'name':'赵森','age':19, 'depart':https://www.luffycity.com/api/v1/depart/1/},
    				{'id':1,'name':'赵森','age':19, 'depart':https://www.luffycity.com/api/v1/depart/1/},
    				{'id':1,'name':'赵森','age':19, 'depart':https://www.luffycity.com/api/v1/depart/1/},
    			]
    		}
    	
    	https://www.luffycity.com/api/v1/users
    		{
    			code:1000,
    			data:[
    				{'id':1,'name':'赵森','age':19, 'depart_title':'公关部'},
    				{'id':1,'name':'赵森','age':19, 'depart_title':'公关部'},
    				{'id':1,'name':'赵森','age':19, 'depart_title':'公关部'},
    				{'id':1,'name':'赵森','age':19, 'depart_title':'公关部'},
    				{'id':1,'name':'赵森','age':19, 'depart_title':'公关部'},
    			]
    		}
    

     

      

  • 相关阅读:
    【数据库】数据表解锁
    【数据库】Oracle数据备份恢复
    【数据库】mysql没有密码无法登录的解决办法
    【设计模式】设计模式(一)-- 大话设计模式读书笔记
    【Linux】Linux常用命令及操作 (一)
    【GIT】git详解
    【python】Anaconda4 linux/windos安装
    【心 得】关于此博客
    eclipse中的.project 和 .classpath文件的具体作用
    Oracle忘记密码处理
  • 原文地址:https://www.cnblogs.com/LearningOnline/p/9429311.html
Copyright © 2020-2023  润新知