Use the Django and REST Frame­works to Cre­ate a Sim­ple API

This ar­ti­cle is a short tu­to­rial on how to cre­ate a sim­ple TaskAPI with SQLite, which com­mu­ni­cates over JSON and REST. We will use the Django Web and REST frame­works.

OpenSource For You - - CONTENTS -

Nowa­days, we live in a multi-plat­form world. Ev­ery­one has at least one com­puter, tablet, smart­phone or smart­watch. Our little gad­gets store and re­trieve data. What a great op­por­tu­nity this presents to touch bil­lions of peo­ple with ex­cel­lent ap­pli­ca­tions, which even those with­out any com­puter knowl­edge can han­dle.

What­ever front-end you can think of, on which­ever plat­form, you still need a way to store data. The back-end is the heart of ev­ery sim­ple and com­plex ap­pli­ca­tion.

In this ar­ti­cle, we will dis­cuss how to cre­ate a sim­ple TaskAPI with SQLite, which com­mu­ni­cates over JSON and REST. We will use the Django Web and REST frame­works.

Vir­tual en­vi­ron­ment and pro­ject set-up

First of all, it is a good habit to set up a new Python vir­tual en­vi­ron­ment to quar­an­tine the re­quire­ments from the rest of the sys­tem. You can in­stall vir­tualenv and all the other re­quire­ments over PyPI.

I as­sume you have al­ready in­stalled Python 3.X and vir­tualenv.

$ mkdir Taskpro­ject $ cd Taskpro­ject $ vir­tualenv env $ source env/bin/ac­ti­vate # On Win­dows use ‘env\ Scripts\ac­ti­vate’

Now, we are in our vir­tual en­vi­ron­ment and ev­ery re­quire­ment we want to in­stall will be placed in­side the env folder. Let’s in­stall the nec­es­sary re­quire­ments in spe­cific ver­sions via Pip.

In this ar­ti­cle, we use Python 3.X, Django 1.9 and the Django REST frame­work 3.0.

-# In­stall Django and Django REST frame­work into the vir­tualenv $ (env) pip in­stall django $ (env) pip in­stall djan­gorest­frame­work # by de­fault PIP in­stall the lat­est ver­sion. Cre­at­ing a Django pro­ject and app

A Django pro­ject can man­age mul­ti­ple Django ap­pli­ca­tions. We are go­ing to cre­ate a pro­ject called TaskAPI, and an ap­pli­ca­tion called Task in­side the TaskAPI pro­ject.

$ (env) django-ad­min.py start­pro­ject TaskAPI $ (env) cd TaskAPI $ (env) django-ad­min.py star­tapp Task Ad­just­ing the pro­ject set­tings

The set­tings are de­fined in the file /TaskAPI/set­tings.py. First, we have to add the in­stalled apps. For our ap­pli­ca­tion, we need to in­stall the Task app along with the manda­tory Django ap­pli­ca­tion, and the Django REST frame­work, rest_frame­work.

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.