web接口
'''
ak: 6E823f587c95f0148c19993539b99295
region: 上海
query: 肯德基
output: json
'''
RESTful规范
'''
1)通常使用https请求
2)域名:有api关键字出现
-- https://api.example.com (存在跨域问题)
-- https://example.com/api
3)版本:不同版本需要标注
-- https://example.com/api/v1 | -- https://example.com/api/1
-- https://example.com/api/v2 | -- https://example.com/api/2
4)资源:请求的目标数据称之为资源,资源一般都有名词复数表示
-- https://example.com/api/v1/books (之前不规范的案例: /get_books/)
5)操作方式:不从请求链接体现操作方式,从请求方式上决定操作方式
-- get:https://example.com/api/v1/books 获取所有
-- post:https://example.com/api/v1/books 新增一本
-- put:https://example.com/api/v1/book/1 更新id=1的一本
-- patch:https://example.com/api/v1/book/1 更新id=1的一本
-- delete:https://example.com/api/v1/book/1 删除id=1的一本
6)资源过滤:通过接口传递参数来过滤资源
-- https://example.com/api/v1/books?limit=10 限制10条
7)状态码:返回数据要标准状态码,通过在数据中 {"status": 200}
-- SUCCESS("0", "查询成功")
-- NODATA("1xx", "非正确,无数据,显示基本信息")
-- FEAILED("2xx", "查询失败")
8)错误信息:请求失败需要标注错误信息 {"message": "请求参数不合法"}
9)操作结果:请求操作成功的返回结果 {"results": []}
-- get:返回资源列表 | 返回单一资源
-- post:返回单一新增资源
-- put:返回更新的资源
-- patch:返回更新的资源
-- delete:返回空文档
10)子资源返回资源接口:返回的资源如果有子资源,返回子资源的链接地址,如查找书,书的封面图片就可以url表示
'''
drf简易书写TESTful规范接口
from app import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^cbv/', views.CBVTest.as_view()),
url(r'^books/', views.books),
url(r'^book/(?P\d+)/', views.book),
]
from django.http import JsonResponse
book_list = [{
'id': 1, 'name': '红楼梦'}, {
'id': 2, 'name': '水浒传'}]
def books(request):
if request.method == "GET":
if 'ak' not in request.GET:
return JsonResponse({
'status': '101',
'msg': 'ak不存在'
}, json_dumps_params={
'ensure_ascii': False})
ak = request.GET.get('ak')
if ak != '123abc':
return JsonResponse({
'status': '200',
'msg': 'ak非法'
}, json_dumps_params={
'ensure_ascii': False})
return JsonResponse({
'status': '0',
'msg': 'ok',
'results': book_list
}, json_dumps_params={
'ensure_ascii': False})
if request.method == 'POST':
name = request.POST.get('name')
id = len(book_list) + 1
book = {
'id': id, 'name': name}
book_list.append(book)
return JsonResponse({
'status': '0',
'msg': 'ok',
'results': book
}, json_dumps_params={
'ensure_ascii': False})
CBV源码分析
from django.shortcuts import render, HttpResponse
from django.views import View
class CBVTest(View):
def dispatch(self, request, *args, **kwargs):
pass
super().dispatch(request, *args, **kwargs)
pass
def get(self, request):
return render(request, 'cbv.html')
def post(self, request):
return HttpResponse('cbv post method')
<form action="/cbv/" method="post">
{% csrf_token %}
<input type="text" name="usr">
<button type="submit">提交button>
form>
from app import views
urlpatterns = [
url(r'^cbv/', views.CBVTest.as_view()),
]
drf安装与使用
from rest_framework.views import APIView
from rest_framework.response import Response
user_list = [{
'id': 1, 'name': 'Bob'}, {
'id': 2, 'name': 'Tom'}]
class Users(APIView):
def get(self, request, *args, **kwargs):
return Response({
'status': 0,
'msg': 'ok',
'results': user_list
})
def post(self, request, *args, **kwargs):
name = request.data.get('name')
id = len(user_list) + 1
user = {
'id': id, 'name': name}
user_list.append(user)
return Response({
'status': '0',
'msg': 'ok',
'results': user
})
from app import views
urlpatterns = [
url(r'^users/', views.Users.as_view()),
]
request源码分析
view = super(APIView, cls).as_view(**initkwargs)
return csrf_exempt(view)
request = self.initialize_request(request, *args, **kwargs)
self.initial(request, *args, **kwargs)
self.perform_authentication(request)
self.check_permissions(request)
self.check_throttles(request)
drf序列化
初级
class Students(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField()
gender = models.CharField(max_length=8)
def __str__(self):
return 'Student: %s' % self.name
from rest_framework import serializers
class StudentSerializer(serializers.Serializer):
id = serializers.IntegerField()
name = serializers.CharField()
age = serializers.IntegerField()
gender = serializers.CharField()
from app import models
from app.my_serializer import StudentSerializer
class Students(APIView):
def get(self, request, *args, **kwargs):
stus = models.Students.objects.all()
stu_ser = StudentSerializer(stus, many=True)
print(stus)
return Response({
'status': 0,
'msg': 'ok',
'results': stu_ser.data
})
from app import views
urlpatterns = [
url(r'^students/', views.Students.as_view()),
]