Use the Django and REST Frameworks to Create a Simple API
This article is a short tutorial on how to create a simple TaskAPI with SQLite, which communicates over JSON and REST. We will use the Django Web and REST frameworks.
Nowadays, we live in a multi-platform world. Everyone has at least one computer, tablet, smartphone or smartwatch. Our little gadgets store and retrieve data. What a great opportunity this presents to touch billions of people with excellent applications, which even those without any computer knowledge can handle.
Whatever front-end you can think of, on whichever platform, you still need a way to store data. The back-end is the heart of every simple and complex application.
In this article, we will discuss how to create a simple TaskAPI with SQLite, which communicates over JSON and REST. We will use the Django Web and REST frameworks.
Virtual environment and project set-up
First of all, it is a good habit to set up a new Python virtual environment to quarantine the requirements from the rest of the system. You can install virtualenv and all the other requirements over PyPI.
I assume you have already installed Python 3.X and virtualenv.
$ mkdir Taskproject $ cd Taskproject $ virtualenv env $ source env/bin/activate # On Windows use ‘env\ Scripts\activate’
Now, we are in our virtual environment and every requirement we want to install will be placed inside the env folder. Let’s install the necessary requirements in specific versions via Pip.
In this article, we use Python 3.X, Django 1.9 and the Django REST framework 3.0.
-# Install Django and Django REST framework into the virtualenv $ (env) pip install django $ (env) pip install djangorestframework # by default PIP install the latest version. Creating a Django project and app
A Django project can manage multiple Django applications. We are going to create a project called TaskAPI, and an application called Task inside the TaskAPI project.
$ (env) django-admin.py startproject TaskAPI $ (env) cd TaskAPI $ (env) django-admin.py startapp Task Adjusting the project settings
The settings are defined in the file /TaskAPI/settings.py. First, we have to add the installed apps. For our application, we need to install the Task app along with the mandatory Django application, and the Django REST framework, rest_framework.