REST APIs In Django | How To Set Up Project In Django | A Comprehensive Guide to Setting Up REST APIs in Django
Ahmad Raza
Undergrad SoftwareEngineer | CGPA 3.83/4.00 | Global Nominee @NASA?? | 15X Inte'l Hackathons & Hackathon Finalist | LeetCode 350+ | 1X Hackathon Mentor | IELTS & AI Instructor | MERN Stack & Gen AI developer | C++ Python
What are REST APIs?
REST APIs (Representational State Transfer APIs) are a specific type of API (Application Programming Interface) that follow a set of architectural principles and constraints to allow communication between different systems over the internet.
What is Django?
So, basically Django is an open source liberary and a frame work for web applications and development.
What is frame Work?
So, framework is a structured and well mannered plateform to create applications on web.Instead of starting from the scratch, we can use already planned components, built in functions which provides us foundation to create applications of our choice.
So, its just like a foundation having built in components and we can create something by using these pre-built tools.These frameworks allow us to make reusable components, which we can reuse for other applications too.
Steps To Set Up New Django Project?
If you are using Global Interpreter, than installation of django will in the python directory, so every newly created project and infact all the projects can access this installed component and thus there is no need to install this for every single project.
"python manage.py migrate"
If you are using Virtual Interpreter, than installation of django will be only for this project, no other projects can access this installed django
"python manage.py migrate"
Explanation
When we run command django-admin startdjango myApp, django creats a new project with the name myApp.
With default directory structure, where in the main path we have manage and myApp named directory.
In myApp/ we have further few files. Like urls and many others
Now to create our django app, we will run the commnad "python manage.py startapp hotel"
This command creates a new directory named hotel with the following structure:
hotel/
├── init
领英推荐
├── admin
├── apps
├── migrations/
│ └── init
├── models
├── tests
└── views
In models file we basically code classes where each class presents a database table, and the fields in the class represents the columns in the DataBase table. So, models are the classes that basically represents structure of your database tables.
In views file, basically we are getting the HTTP requests and we are returning the HTTP responses. So, they take user inputs, interact with the model and return responses based on current database models. Here, we are getting requests and after interacting we are returning responses.
In apps. file, A configuration file for the app. This file contains the app's configuration class, which is used by Django to configure some aspects of the app.
In migrations/ directory, this directory contains migration files that Django generates to manage changes to your models (like adding a new field or modifying an existing one.
The init.py file in this directory marks it as a Python package.
And there are two more files, which we add ownself, and these two are:
├── serializers
└── urls
In urls file, we basically integrating our requests with views, when we made a request on any Platform, like Postman. The django goes to urls file, match the request url with urls file urls, and then goes to views fileto call the respective veiw.
In serializers file, we actually converting our response into JSON formate.
To Test REST APIs Use Postman
After you add your database fields, tables, data and you confirmed you urls, and all set. Then you can test your REST APIs by just following some steps:
Live Example
To learn more and to check how it works, in which files we have what content, and how we made requests and all these things, you can check my GitHub repository, here I created a setup for Hotel Booking and airlines, I made models and urls there, you can check that too, here is the link : Hotel Mangement REST API Example