OpenSource For You

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 communicat­es 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 opportunit­y this presents to touch billions of people with excellent applicatio­ns, 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 applicatio­n.

In this article, we will discuss how to create a simple TaskAPI with SQLite, which communicat­es over JSON and REST. We will use the Django Web and REST frameworks.

Virtual environmen­t and project set-up

First of all, it is a good habit to set up a new Python virtual environmen­t to quarantine the requiremen­ts from the rest of the system. You can install virtualenv and all the other requiremen­ts over PyPI.

I assume you have already installed Python 3.X and virtualenv.

$ mkdir Taskprojec­t $ cd Taskprojec­t $ virtualenv env $ source env/bin/activate # On Windows use ‘env\ Scripts\activate’

Now, we are in our virtual environmen­t and every requiremen­t we want to install will be placed inside the env folder. Let’s install the necessary requiremen­ts 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 djangorest­framework # by default PIP install the latest version. Creating a Django project and app

A Django project can manage multiple Django applicatio­ns. We are going to create a project called TaskAPI, and an applicatio­n called Task inside the TaskAPI project.

$ (env) django-admin.py startproje­ct 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 applicatio­n, we need to install the Task app along with the mandatory Django applicatio­n, and the Django REST framework, rest_framework.

 ??  ??
 ??  ??

Newspapers in English

Newspapers from India