The world of technology is evolving at a rapid pace. In the last decade, we have achieved some of the most revolutionary milestones in terms of tech advancements. We finally managed to give shape to an age-old concept called Artificial Intelligence (AI) and machine learning through the tools, frameworks and tech concepts available today.
Thanks to this, we are now making an impact across sectors like healthcare, automobile, aviation, military and defence, entertainment and more. If we had to boil down the evolution of all this to a handful of tools, Python would definitely find an irreplaceable place on the list.
A high-level programming language, Python web development has been deployed for AI, data science, machine learning projects, and more. Ideal for both veterans and freshers, Python has been leading the way with its features and functionalities.
That’s why we felt it was required to further spread knowledge on Python and its technicalities for faster evolution. In this post, we will explore how we could build an API in Python with Django.
As far as Django is concerned, it is one of the popular frameworks for Python API development. This article is to help you create a simple API to fetch data from python using the get or post method. As part of this, we will be covering the following topics:
- Starting a Django project
- Installing Django
- Creating a Super user
- Delivering a JSON to a requester from API.
- Running the Project
- Testing API with Postman
Building a REST API in Django is easy. In this tutorial, we will walk you through the steps to get your first API up and running.
Why Django REST API?
Before we get to the code, it’s worth considering why you would want to build an API. An API is a standardized way to provide data to other applications or get data from a server. Those applications can then use the data however they want. Sometimes, APIs also offer a way for other applications to make changes to the data.
Starting A Django Project
First, we’re going to create a new Django project named rapid-api-practice. Then, within that project, we will create a new app called API. Although this may seem odd at first, the “Django way” is to house an app, or more than likely multiple apps, within a single “project.”
We’ll put the whole thing in a root directory with the project’s name from the command line, like the following:
- mkdir rapid-api-practice
- cd rapid-api-practice
Then, we’ll install Django.
To Install Django, open the command prompt and use the below command:
- pip install django
And finally, we can set up our new project with our single application, my-site. Notice them. In the command, which is telling Django to place the project in your rapid-api-practice directory.
- Django-admin startproject mysite
Let’s look at what start project created:
Next, from within the newly created polls project directory, run:
- Django-admin startapp polls
That should leave you with a file tree that should now look something like this:
Next, we can set up our database by running our first migration:
- Python manage.py makemigrations
- Python manage.py migrate
Finally, we’ll need to create our first user. Let’s call this user admin, and set the user’s password to password. From the terminal, run:
#1. Creating Superuser:
- python3 manage.py createsuperuser
Email address: email@example.com
Password (again): ********
This password is too common.
Bypass password validation and create a user anyway? [y/N]: y
Superuser created successfully.
#2. Delivering a JSON to a requester from API
- Views.py (polls/views.py):
Now, we’re going to define our views (or, more specifically, viewsets) in order to send our data from our backend to the browser. Open polls/views.py and insert the following:
So, for standard CRUD operations on an SQL database, Django accepts and handles GET, POST, PUT and DELETE requests. They also allow for a single endpoint to handle requests for list views of objects in the database as well as individual object instances. Pretty sweet, really!
#3. URLS.py (polls/urls.py):
To do so, we’ll import include and path from the django.polls/urls module, as well as routers from Django Rest Framework and, of course, the views that are to be returned. We can accomplish all of this by including the following code in our urls.py file.
- Settings.py (mysite/settings.py):
Our API and Django Rest Framework are just such apps. In order to make use of them in our Django project, we’ll have to add ‘polls’ and ‘rest_framework’ to the INSTALLED_APPS list in mysite/settings.py, like so:
#4. Run Project:
- Python manage.py runserver
Starts a lightweight development Web server on the local machine. By default, the server runs on port 8000 on the IP address 127.0.0.1. You can pass in an IP address and port number explicitly.
If you run this script as a user with normal privileges (recommended), you might not have access to start a port on a low port number. Low port numbers are reserved for the superuser (root).
5. Test API with Postman:
- Send Request from postman
- Get result from postman
Python will continue to rule the space for the coming years and mastering it is inevitable to become a dependable source. We hope you enjoyed this sweet and small tutorial. APIs can get much more complicated with multiple endpoints with more complex queries.
Have fun building some new APIs!
Also, if you intend to have experts work on your Python web development projects, get in touch with a reliable Python development company like DEV Information Technology Ltd. They are your ideal collaborators.
Latest posts by Manthan Tanna (see all)
- A Kickstarter Guide to Building an API In Python with Django - November 24, 2021