How to creating a Simple ToDo Api With Django

How to creating a Simple ToDo Api With Django

Creating a Simple ToDo Api With Django - In this tutorial, we'll create a simple todo api with Python 3 and Django 2.

In this tutorial, we'll create a simple todo api with Python 3 and Django 2.

Installing the requirements

Create a virtual environment and install Django.

$ virtualenv venv # creates a virtual environment
$ source venv/bin/activate # activates the virtual environment
(venv)$ pip install Django

That's all we'll need for now.

Creating the project

Use the Django startproject command to create the project we'll be working on.

$ django-admin startproject todoapi

This creates a todoapi directory with generated files to start us off. The directory should look something like this:

Databse setup

To keep things simple, we'll use the SQlite database, so we don't have to install anything since it comes with Python. In addition, that's already setup for us in todoapi/ Have a look at the code and you should see something similar to this:

# Database

    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

Creating models

To create models for our database, we first need to create a todos "app". This is where we'll implement our features.

$ python startapp todos

This will create a todos directory with the following files:


This app should be added to the todoapi/


Ok, now that Django knows about our app, open the file to add the models. We'll only create one model called Todo: from django.db import models

# Create your models here.
class Todo(models.Model):
    todo = models.CharField(max_length=100, null=False,
                            help_text="This field is required")
    done = models.BooleanField(default=False)

This model will create a table for us with todo and done columns to save our data. The todo column will save the actual todo, and the done column will save the status -- whether it's done or not. Notice that it is a BooleanField, meaning we'll save True when done or False when not done, which is the default.

Now all we need to do is run makemigrations to notify Django of our models, and migrate to do the actual migration.

$ python makemigrations todos
$ python migrate

Creating the endpoints

We'll have two endpoints for now:

  • /todos to GET a list of all our todos
  • /todos/ to GET the todo matching the id

To test this out, we'll create two simple views in the todos/ file.

from django.http import JsonResponse
from django.shortcuts import get_object_or_404

from .models import Todo

# Create your views here.
def todos(request):
    todos_list = Todo.objects.all()
    data = {
        "todos": list(todos_list.values(
            "todo", "done"
    return JsonResponse(data)

def single_todo(request, pk):
    todo = get_object_or_404(Todo, pk=pk)
    data = {
        "todo": todo.todo,
        "done": todo.done
    return JsonResponse(data)

Create a new file called in your todos app and add the following code:

# todos/
from django.urls import path

from .views import todos, single_todo

urlpatterns = [
    path("todos/", todos, name="todos"),
    path("todos/<int:pk>", single_todo, name="single_todo")

Go to todoapi/ and include the todos url you just created:

# todoapi/
from django.contrib import admin
from django.urls import path, include  # add include here

urlpatterns = [
    path('', include('todos.urls'))  # add this

Set up your admin and test the endpoints

Create a superuser:

$ python createsuperuser

Next, register your models in the /todos/ file so you can have access to them from the admin dashboard:

from django.contrib import admin
from .models import Todo

# Register your models here.

That's it! Now run the server and go to Log in with the details you registered for the superuser above to add your todos. This is image title

You can retrieve the todos created via a rest client via the endpoints

python django

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Python Django Tutorial | Django Course

🔥Intellipaat Django course: 👉This Python Django tutorial will help you learn what is django web development &...

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Developing Restful APIs with Python, Django and Django Rest Framework

This article is a definitive guide for starters who want to develop projects with RESTful APIs using Python, Django and Django Rest Framework.

How to run a python script in django propject which has multiple django models in it

I have a script that I want to run from terminal inside a django project and that script is a python script and it has multiple django models in it .So how can I run that script since I am not able to run it directly.Also everything resides inside docker container.