def task_lists(request): if request.method == 'GET': t_lists = TaskList.objects.all() serializer = Task(t_lists, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': t_list = json.loads(request.body) serializer = Task(data=t_list) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors)
def task_list(request, pk): try: t_list = models.TaskList.objects.get(id=pk) except models.TaskList.DoesNotExist as e: return JsonResponse({'error': str(e)}, safe=False) if request.method == 'GET': serializer = Task(t_list) return JsonResponse(serializer.data, safe=False) elif request.method == 'PUT': data = json.loads(request.body) serializer = Task(instance=t_list, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.errors) elif request.method == 'DELETE': t_list.delete() return JsonResponse({})