Step 1: Import View
from django.views import View
Step 2: Create this base class
class CLS_executer(View):
db = DB_executer
def get(self, request):
executer_list = list(self.db.objects.all().values())
return HttpResponse(json.dumps(executer_list), content_type='application/json')
def post(self, request):
data = json.loads(request.body)
self.db.objects.create(**data)
return self.get(request)
def put(self, request):
data = json.loads(request.body)
self.db.objects.filter(id=int(data['id'])).update(**data)
return self.get(request)
def delete(self, request):
id = request.GET['id']
self.db.objects.filter(id=int(id)).delete()
return self.get(request)
Step 3: Set up the class view mapping in urls.py
urlpatterns = [
path('executer/', CLS_executer.as_view())
]
Step 4: Verification
The get method directly requests the browser: {server ip: port number}/executer. Post, put, and delete can be verified with postman.