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':'公关部'},
			]
		}

 

  

转载于:https://www.cnblogs.com/LearningOnline/p/9429311.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值