restful规范标准与Django rest framework组件对应

rest framework规范标准;(建议)
1、根据method不同,进行不同操作
GET/POST/PUT/DELETE/PATCH

对应Django:视图
def get(self, request):
pass

2、面向资源编程
http://www.hello.com/salary
salary 名称
对应Django:路由
urls的路由名称取名称

3、体现版本
http://www.hello.com/v1/salary
http://www.hello.com/v2/salary
对应Django:版本
1、添加setings.py 配置
组件注册
INSTALLED_APPS = [
'rest_framework'
]
REST_FRAMEWORK = {
'DEFAULT_VERSIONING_CLASS':'rest_framework.versioning.URLPathVersioning',
'ALLOWED_VERSIONS':['v1','v2'], # 允许的版本
'VERSION_PARAM':'version', # 参数
'DEFAULT_VERSION':'v1', # 默认版本
....
}
2、设置urls路由
app/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

4、体现是API(接口)
http://www.hello.com/api/v1/salary
http://www.hello.com/api/v2/salary
5、https 协议(安全)
https://www.hello.com/api/v1/salary
https://www.hello.com/api/v2/salary
6、响应式设置状态码
200
300
400
500
# return HttpResponse('ok', status=200)
7、条件
https://www.hello.com/api/v2/salary?page=1&size=10
8、返回值
https://www.hello.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:删除
None
9、返回错误信息
{
code: 100001,
error: '账号密码错误'
}
10、Hypermedia API
ret = {
code: 1000,
data:{
id:1,
name:'小强',
*** depart_id:http://www.luffycity.com/api/v1/depart/8/
}
}

posted @ 2021-07-27 10:05  Feng_fu  阅读(61)  评论(0)    收藏  举报