Python, one of the most popular programming languages today, is used for almost every subject in the field of informatics. Although we often hear the name of Python in the fields of artificial intelligence and data analysis, the use of Python is not limited to this. Especially when developing the REST API, Python’s Django Rest Framework provides developers with many conveniences.
What is Django Rest Framework
Django Rest Framework is an advanced framework that allows developers to code a common backend for both mobile applications, web applications, and desktop applications. For example, you can use a backend server that you coded with Django Rest Framework both in your mobile application and in your web application.
You can develop your applications by using Django Rest Framework with frontend technologies such as Angular, React, Vue. Since Django Rest Framework gives responses in a common structure in programming, you can use these outputs either with Angular or with React. So what type of output are these? Structures like JSON, of course. You can send these printouts in different structures upon request, of course, to the front end.
Creating a REST API in Django Rest Framework
Before you start developing the application, the Django Rest Framework library must be installed on your computer. If it is not installed, you can install it with the following command.
Then we create a project with the following command.
We are creating another layer in the default project with the following command, where we will perform REST API operations.
After the API layer is created, we add “rest_framework” and “API” values to the INSTALLED_APPS section in the Django project layer.
Now let’s move on to preparing the REST API. Let’s paste the following into the API/views.py file.
Now let’s paste the following into the djangoProject/urls.py file.
Yes, we have added the REST API method we created to the project as a url and everything is ready to test.
Let’s run the following command from the command line.
After the application runs successfully, when we send an HTTP GET request to http://localhost:8000/helloworld/ via Postman, the response will be as follows.
That’s how easy it is to quickly create a REST API in minutes using Python’s Django Rest Framework. You can also prefer Django Rest Framework for your new projects.