快速生成API,后端分离。
1.使用了 https://github.com/zmrenwu/django-blog-tutorial.git 该项目作为模板,
教程参考:一份官方文档:http://www.django-rest-framework.org/tutorial/1-serialization/
一份gitbook文档(翻译版):https://whatwewant.gitbooks.io/django-rest-framework-tutorial-cn/content/1.Serialization.html
两份文档在代码上存在略微差异,需要细细比较,对照。
2.学习部分:
一、使用serializer,序列化:
serializers.py: 存在手动serializer 每个model 的field,也存在快速serializer。
views.py: 定义list和detail的方法(使用JOSNResponse响应数据以及状态码)
urls.py; 更新url
二、使用 REST框架的 Response 和 APIView
views.py: 使用Response ,用装饰器 @api_view ,在方法中添加format格式
urls.py: 更新urlpatterns,添加格式后缀模式format_suffix_patterns(urlpatterns)