FBV(function base views) 就是在视图里使用函数处理请求。
CBV(class base views) 就是在视图里使用类处理请求。
视图函数中代码
from django.http import JsonResponse, QueryDict
from django.shortcuts import render
# Create your views here.
from django.views import View
from .models import *
class BookAPI(View):
def get(self, req):
print(req.method)
return JsonResponse({'code': 200, 'msg': 'successful'})
def post(self, req):
params = req.POST
title = params.get('title')
content = params.get('content')
price = params.get('price')
poem = Poem.objects.create(
title=title,
content=content,
price=price,
)
return JsonResponse({'title': poem.title})
def put(self, req):
params = QueryDict(req.body)
id = int(params.get('id'))
titl