def book_list(request):
"""
List all books, or create a new book.
"""
if request.method == 'GET':
books = Book.objects.all()
serializer = BookSerializer(books, many=True)
return JsonResponse(serializer.data, safe=False)
elif request.method == 'POST':
data = JSONParser().parse(request)
serializer = BookSerializer(data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.errors, status=400)
# Returns a Json response for the book of id pk if the request is a GET request.
# Adds a new book to the database if the request is a PUT request.
# Deletes book of id pk if the request is a DELETE request.
评论列表
文章目录