REST APIs In Django | How To Set Up Project In Django | A Comprehensive Guide to Setting Up REST APIs in Django

REST APIs In Django | How To Set Up Project In Django | A Comprehensive Guide to Setting Up REST APIs in Django

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.

  • Open you Command Prompt
  • Run "pip install django"
  • Navigate to your project directory "cd cd path/to/your/directory"
  • Run "django-admin startdjango myProject"
  • Run migration commands, these commands are used to create new migration files as you change something in your project
  • "python manage.py makemigrations"

"python manage.py migrate"

  • Run the command to run server "python mange.py run server"

If you are using Virtual Interpreter, than installation of django will be only for this project, no other projects can access this installed django

  • Open you Command Prompt
  • Navigate to your project directory "cd cd path/to/your/directory"
  • Run "python3 -m venv myenv"
  • Run "myenv\Scripts\activate"
  • Run "pip install django"
  • Run "django-admin startdjango myProject"
  • Run migration commands, these commands are used to create new migration files as you change something in your project
  • "python manage.py makemigrations"

"python manage.py migrate"

  • Run the server "python mange.py run server"

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 views file we have two type of requests, POST and GET,
  • In POST we are actually getting input data from the user and then we are returning HTTP response based on what we have.
  • In GET, we are not getting any kind of input, we just reteriving the HTTP response.

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:

  • Download Postman by visiting this link, Download Postman
  • Now click on new or + button, you can see on top middle section
  • Select want type of request you want to make, like GET or POST.
  • Add your request URL
  • If request type is GET, then just click send. And the sign for correct request is that you will got some output in the form of JSON.
  • If you are going to test for POST request, you have to write respective data in JSON format in body section during making request, because in POST type requests, users have to send some data, like if user wants to make request to book a hotel room, he has to send request to hotel server, with data of room no. Like room no. 1


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


要查看或添加评论,请登录

Ahmad Raza的更多文章

社区洞察

其他会员也浏览了