Building web APIs with django

Building web APIs with django

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 meetup.com.

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
  2. 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
  3. 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 manage.py startapp accounts

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

./eventScheduler
├── accounts
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── eventScheduler
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-35.pyc
│   │   └── settings.cpython-35.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

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.

Thanks.

Sponsors

Please note that some of the links below are affiliate links and at no additional cost to you. Know that I only recommend products, tools and learning services I've personally used and believe are genuinely helpful. Most of all, I would never advocate for buying something you can't afford or that you aren't ready to implement.

Scraper API

Scraper API is a startup specializing in strategies that'll ease the worry of your IP address from being blocked while web scraping.They utilize IP rotation so you can avoid detection. Boasting over 20 million IP addresses and unlimited bandwidth.

In addition to this, they provide CAPTCHA handling for you as well as enabling a headless browser so that you'll appear to be a real user and not get detected as a web scraper. Usage is not limited to scrapy but works with requests, BeautifulSoup and selenium in the python ecosystem. Integration with other popular platforms such as node.js, bash, PHP and ruby is also supported. All you have to do is concatenate your target URL with their API endpoint on the HTTP get request then proceed as you normally would on any web scraper. Don't know how to webscrape? Don't worry, I've covered that topic extensively on the webscraping series. All entirely free!

scraperapi

Using this scraper api link and the promo code lewis10, you'll get a 10% discount on your first purchase!! You can always start on their generous free plan and upgrade when the need arises.

Digital Ocean

Looking for cloud hosting for your projects? Digital Ocean is just the right partner for you.

They make it simple to launch in the cloud and scale up as you grow – with predictable pricing, team accounts, and more. Their intuitive control panel and API give you time to build more and spend less time managing your infrastructure.

Using this digitalocean link to sign up, you'll get $100 in credit for the first two months!

Share to social media