How to Create a Basic API View in Django REST framework

Django REST framework is a powerful and flexible toolkit for building Web APIs. It provides a full suite of features for developing RESTful APIs, including serialization, authentication, and authorization. In this tutorial, we will learn how to create a basic API view in Django REST framework.

Install Django REST Framework

The first step is to install Django REST framework. To do this, open a terminal window and run the following command:

pip install djangorestframework

This will install the latest version of Django REST framework. Once the installation is complete, you can verify that it is installed correctly by running the following command:

python -m django --version

This should output the version of Django REST framework that is installed.

Create a Django Project

The next step is to create a Django project. To do this, open a terminal window and run the following command:

django-admin startproject myproject

This will create a new Django project called “myproject” in the current directory. Once the project is created, you can verify that it is working correctly by running the following command:

python manage.py runserver

This will start the Django development server. You can then open a web browser and navigate to http://127.0.0.1:8000/ to view the default Django page.

Create an App

The next step is to create an app. To do this, open a terminal window and run the following command:

python manage.py startapp myapp

This will create a new app called “myapp” in the current directory. Once the app is created, you can verify that it is working correctly by running the following command:

python manage.py runserver

This will start the Django development server. You can then open a web browser and navigate to http://127.0.0.1:8000/myapp/ to view the default Django page.

Add the App to the Project

The next step is to add the app to the project. To do this, open the settings.py file in the project directory and add the following line:

INSTALLED_APPS = [ ... 'myapp',]

This will add the app to the project. You can then verify that it is working correctly by running the following command:

python manage.py runserver

This will start the Django development server. You can then open a web browser and navigate to http://127.0.0.1:8000/myapp/ to view the default Django page.

Create a Serializer

The next step is to create a serializer. A serializer is a class that is used to convert a Python object into a JSON object. To do this, open the serializers.py file in the app directory and add the following code:

from rest_framework import serializersclass MySerializer(serializers.Serializer): name = serializers.CharField() age = serializers.IntegerField()

This will create a serializer class called “MySerializer”. This class will be used to convert a Python object into a JSON object.

Create a View

The next step is to create a view. A view is a class that is used to handle requests and return responses. To do this, open the views.py file in the app directory and add the following code:

from rest_framework.views import APIViewfrom rest_framework.response import Responsefrom .serializers import MySerializerclass MyView(APIView): def get(self, request): data = { 'name': 'John Doe', 'age': 42 } serializer = MySerializer(data) return Response(serializer.data)

This will create a view class called “MyView”. This class will handle requests and return responses.

Add the View to the URLconf

The next step is to add the view to the URLconf. To do this, open the urls.py file in the project directory and add the following line:

urlpatterns = [ ... path('myview/', MyView.as_view()),]

This will add the view to the URLconf. You can then verify that it is working correctly by running the following command:

python manage.py runserver

This will start the Django development server. You can then open a web browser and navigate to http://127.0.0.1:8000/myview/ to view the view.

Test the View

The final step is to test the view. To do this, open a terminal window and run the following command:

curl http://127.0.0.1:8000/myview/

This will send a request to the view and return the response. The response should be a JSON object containing the data that was passed to the serializer.

Conclusion

In this tutorial, we learned how to create a basic API view in Django REST framework. We installed Django REST framework, created a Django project, created an app, added the app to the project, created a serializer, created a view, added the view to the URLconf, and tested the view. With this knowledge, you should be able to create your own API views in Django REST framework.

Useful Links