DEV Community

Cover image for Building web APIs with django
Lewis kori
Lewis kori

Posted on • Updated on • Originally published at

Building web APIs with django

Originally posted on my website.


Hey there, I'd like to introduce you to making web APIs with the popular python web framework, Django.

You've probably heard that the best way to learn is by doing. So in this walkthrough, we'll be building a RESTful API with Django and the django rest framework. The project will be an event scheduler application, sort of like a mini version of Eventbrite or

Key features will be users to create events and other registered users can mark attendance to the created events. We'll also explore how to review an event, putting in place checks to ensure only those members who attended the event can review any given event. Hopefully, by the end of the series, you too can apply some of the concepts here to build your own REST APIs with Django.

You'll find the project's code on github.

The walkthrough will be divided into three major parts for ease of readability so as not to boggle you down with too many concepts at the same time.

  1. Introduction and project setup(You are here).
  2. User registration,authetication and authorization with djoser and JSON web tokens(JWTs).
  3. Events creation and attendance endpoints.

I'll try to follow a similar structure in each post. This will mainly involve:

  1. Model creation.
  2. Serializers, API views and modifying them to suit our needs.
  3. Creating permission classes for our endpoints.
  4. Writing automated unit tests for the endpoints.
  5. Demo with postman.

Project setup

For this project, we'll need to have python 3+ installed. You can find the necessary steps here.
Next, we'll need to make virtual environments for installing project dependencies.

Install virtualenv

pip install virtualenv

Create a new virtualenv

virtualenv yourenvname -p python3.6

Activate virtual environment

source yourenvname/bin/activate

Run this command within the directory the virtual environment was created in.

Once your virtual environment is activated, there's a number of dependencies we'll be needing for the project:

  1. Django and the Django rest framework

pip install django djangorestframework

  1. Djoser: REST implementation of Django authentication system. The Djoser library provides a set of Django Rest Framework views and endpoints to handle basic actions such as registration, login, logout, password reset, and account activation. You could build out the features yourself but this has most of the features you may end up building.

pip install djoser

  1. django-rest-framework-simplejwt: provides a JSON Web Token authentication backend for the Django REST Framework.

pip install djangorestframework_simplejwt

Typically, if you have a lot of project dependencies in python, it's advisable to put them in a requirements.txt file and install them with one command.

pip install -r requirements.txt

To kick off the new django project, run the following once you've activated the previously created virtual environment.

django-admin startproject eventScheduler

Now to start with the first app that we'll be requiring for the next section:

cd eventScheduler
python startapp accounts 
Enter fullscreen mode Exit fullscreen mode

So far so good I hope.
If you've followed the steps along, our app should now be having its structure as

├── accounts
│   ├──
│   ├──
│   ├──
│   ├── migrations
│   │   └──
│   ├──
│   ├──
│   └──
├── eventScheduler
│   ├──
│   ├── __pycache__
│   │   ├── __init__.cpython-35.pyc
│   │   └── settings.cpython-35.pyc
│   ├──
│   ├──
│   └──
Enter fullscreen mode Exit fullscreen mode

Over the next few days, we'll go over the various ways of implementing RESTful APIs with Django, filling in the different sections Indindicated previously.

In case you have any questions, leave a comment below or contact me on twitter and I'll get back to you as soon as possible.


Top comments (0)