My First JSON REST API with Django

Modern days applications compose their main logic or Business logic in APIs. (Application Programming Interface).

APIs are interfaces designed to communicate a system with outside entities. This interfaces may use different specifications, but nowadays one of the most popular specifications is known as JSON REST API.

A JSON REST API is an interface of a system that allows communication to happen normally under the HTTP Protocol and strictly using JSON data format as a communication format.

The Term REST comes from an specific set of rules that provides a Client / Server, stateless, uniform, and cacheable communication, that, as mention before is normally implemented using the popular HTTP communication protocol.

More about REST


Django Framework

Django is a web framework that follows the pattern MVT (Model View Template). It has been around since 2005 and with more than 13 years of development is more than a good option to develop web applications.

Django perse is not built specifically to built JSON REST APIs, is built more for a monolithic approach of websites and applications, but, its large and active community, stability and large set of tools have made it a very popular choice for also building APIs.

In combination with Django Rest Framework it makes it very easy to build a JSON REST API very fast, but also this is possible on its own.

My JSON REST API with Django

This instructions are based on the Oficial Django Documentation

1. Install PIP:

curl -o
pip3 --version

2. Install Django and Django Rest Framework:

pip3 install Django
pip3 install djangorestframework
python3 -m django --version

3. Creating a Django Project

django-admin startproject json_rest_api
python3 runserver

4. Creating my First App

python3 startapp tasks

5. Adding the Apps to the


6. My First View

from django.http import JsonResponse

def index(request):
    return JsonResponse("['Hello World']")


from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),


from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('tasks/', include('tasks.urls')),

7. My First Model

from django.db import models

class Task(models.Model):
    text = models.CharField(max_length=200)

class ToDo(models.Model):
    task = models.ForeignKey(Task, on_delete=models.CASCADE)
    text = models.CharField(max_length=200)
    is_done = models.BooleanField(default=0)
python3 makemigrations tasks
python3 migrate

8. Create a Super User to play with the Models


from django.contrib import admin

from .models import Task, ToDo
python3 createsuperuser
python3 runserver

9. Let’s create more views


from django.urls import path

from . import views

urlpatterns = [
    path('test', views.test, name='index'),
    # All tasks
    path('', views.index, name='index'),
    # ex: /taks/5/
    path('<int:task_id>/', views.task_detail, name='detail'),
    # ex: /tasks/5/vote/
    path('create-task/', views.create_task, name='vote'),


from django.shortcuts import render
from django.http import JsonResponse

from . import models

def test(request):
    json_data = {
        "first_name": "Angel",
        "last_name": "Lacret"
    return JsonResponse(json_data)

def index(request):
    tasks = models.Tasks.objects.all()
    count = models.Tasks.objects.all().count()
    tasks_list = []
    for task in tasks:
        task_object = {}
        task_object["id"] =
        task_object["text"] = task.text
    json_result = {
        "tasks": tasks_list,
        "count": count
    return HttpResponse(json_result)

def task_detail(request, task_id):
    task = models.Tasks.objects.get(id=task_id)
    task_object = {}
    task_object["id"] =
    task_object["text"] = task.text
    return HttpResponse(task_object)

def create_task(request):
    return HttpResponse(response % question_id)




Contact Us

Adress: 66 W Flagler St, Miami, Florida
Phone: +1 (786) 991-3467

© 2020 Cobuild Lab All Rights Reserved.