大纲
链接:https://pan.baidu.com/s/1elpgOu 密码:xi3d
Django REST 框架是一个用于构建 Web API 的功能强大且灵活的工具包。
特点:
Python (2.7, 3.2, 3.3, 3.4, 3.5, 3.6)
Django (1.10, 1.11, 2.0)
前后端分离
Only handles form data. Only works for ‘POST‘ method.
request.data
Handles arbitrary data. Works for ‘POST‘, ‘PUT‘ and ‘PATCH‘ methods.
data: 响应的序列化数据。
status:响应的状态码。默认为200.另请参阅状态代码
......
* status
HTTP_200_OK
HTTP_403_FORBIDDEN
HTTP_404_NOT_FOUND
HTTP_500_INTERNAL_SERVER_ERROR
......
## 类视图
class-based-views
* APIView
* mixins
* generics
## 认证
* 查询 Token
curl -X post http://127.0.0.1/api-token-auth/ -d username=monkey -d password=monkey123
* Token 登录认证
authentication_classes = (TokenAuthentication, )
## 权限
权限列表
* AllowAny
* IsAdminUser
* IsAuthenticated
* IsAuthenticatedOrReadOnly
* DjangoMOdelPermissions
* DjangoModelPermissionsOrAnonReadOnly
* DjangoObjectPermissions
权限认证
permission_classes = (IsAuthenticated,)
## 验证权限
curl http://127.0.0.1/accounts/ -H ‘Authorization: Token 48386f45403c308ba1b2575c546f498c5580efed‘
## 学习资料
官方网站
www.django-rest-framework.org/ (http://www.django-rest-framework.org/)
原文地址:http://blog.51cto.com/51reboot/2077412