Creating a Simple API Rest with Django, Step by Step

Creating a Simple API Rest with Django, Step by Step
Photo by Faisal / Unsplash

Creating a RESTful API with Django can be a powerful tool to expose your application's data and functionality to the world. Django is a high-level Python web framework that allows you to build web applications quickly and efficiently. With the Django REST framework, it's easy to create a RESTful API that follows the best practices of the REST architectural style.

In this process, you'll define your models, create serializers, views, and URLs to create a flexible API that can handle CRUD operations. By the end of this process, you'll have a scalable and secure API that can be integrated into different client applications.

In this tutorial, we'll go through the steps of creating a RESTful API with Django and the Django REST framework, starting with the installation and configuration of the necessary components, and then moving on to the implementation of the core components.

To create an API Rest with Django, you can follow these steps:

Step 1. Install Django and Django REST framework

You can install Django and Django REST framework using pip:

pip install django
pip install djangorestframework

Step 2. Create a Django project

Create a new Django project using the django-admin command:

django-admin startproject myproject

Step 3. Create a Django app

cd myproject
python manage.py startapp myapp

Step 4. Define the models

Define your models in models.py file inside your app.

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)
    published_date = models.DateField()

Step 5. Create serializers

Create serializers for your models to convert them to JSON format.

from rest_framework import serializers
from .models import Book

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = ('id', 'title', 'author', 'published_date')

Step 6. Define the views

Define the views for your API in views.py file inside your app.

from rest_framework import generics
from .models import Book
from .serializers import BookSerializer

class BookList(generics.ListCreateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

class BookDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

Step 7. Define the URLs

Define the URLs for your API in urls.py file inside your app.

from django.urls import path
from .views import BookList, BookDetail

urlpatterns = [
    path('books/', BookList.as_view()),
    path('books/<int:pk>/', BookDetail.as_view()),
]

Step 8. Configure the settings

Add your app and REST framework to the INSTALLED_APPS and REST_FRAMEWORK settings in settings.py file of your project.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
    'rest_framework',
]

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.AllowAny',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ],
}

Step 9. Run the server

Run the development server using the manage.py command

python manage.py runserver

Now you can access your API endpoints at http://localhost:8000/books/ and http://localhost:8000/books/{id}/.

In conclusion, creating a RESTful API with Django is a great way to expose your application's data and functionality to other applications and services.

The Django REST framework provides an efficient way to create a flexible and scalable API that adheres to the REST architectural style. By following the steps outlined in this tutorial, you can create a Django project, define your models, create serializers, views, and URLs to create a fully functional API.

With this API, you can easily integrate your application with other systems and services, opening up new possibilities for collaboration and innovation. So, whether you're building a new application or adding functionality to an existing one, creating a RESTful API with Django is definitely worth considering.

If you're looking to build a new web or mobile application or add functionality to your existing one, but don't have the time or resources to do it yourself, consider 4Geeks' product development service. Our team of experienced developers and designers can help you bring your ideas to life and create a high-quality product that meets your business needs. We use cutting-edge technologies and best practices to ensure that your application is scalable, secure, and user-friendly.

Contact us today to learn more about our product development services and how we can help you build the application of your dreams!