Lädt...

🔧 RESTful API in Django [Django Rest Framework]


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Mastering Django REST Framework: A Guide to Building RESTful APIs
By @bekbrace aka. Amir Bekhit

Hey there, Python developers [and other fellow devs].
If you're diving into web development with Django, adding the Django REST Framework (DRF) to your toolkit will be a game-changer.
It makes building RESTful APIs straightforward and efficient.
In this post, I'll introduce you to RESTful APIs and how to use DRF to build a simple yet powerful library management system (I love books!)
Plus, I've included a video where I demonstrate coding examples to create, edit, delete, and view books.

The Video Guide

Check out my video, where I demonstrate these steps in more detail. You'll see how to create a full CRUD API to manage books, complete with authentication and a browsable UI for testing - if you don't want to watch, continue reading then :)

What is a RESTful API?

RESTful APIs (Representational State Transfer) adhere to architectural principles that ensure flexibility and scalability. The primary idea is to access resources via standard HTTP methods like GET, POST, PUT, and DELETE. Each resource is accessed via a unique URL endpoint.

What is Django REST Framework?

Django REST Framework is a toolkit that seamlessly integrates with Django to build web APIs. Its key features include:

Serialization: Convert data between Python objects and JSON.
Authentication & Permissions: Support for custom and built-in authentication.
Class-Based Views: Reusable views with method-based HTTP request handling.
Browsable API: An interactive UI for testing APIs directly in the browser.

Getting Started with DRF

Install DRF:

Install Django REST Framework with pip.

pip install djangorestframework

Add to Installed Apps:

Update your settings.py file to include 'rest_framework' in the INSTALLED_APPS section.

INSTALLED_APPS = [
    # other apps
    'rest_framework',
]

Build Your Library Management API

Models: Start by defining the book model.

# models.py
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()
    isbn = models.CharField(max_length=13, unique=True)
Serializers: Create a serializer to convert the book model to and from JSON.
# serializers.py
from rest_framework import serializers
from .models import Book

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = '__all__'
Views: Implement views using DRFs class-based views.
# views.py
from rest_framework import viewsets
from .models import Book
from .serializers import BookSerializer

class BookViewSet(viewsets.ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
URLs: Finally, register the viewsets in your URL configuration.
# urls.py
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import BookViewSet

router = DefaultRouter()
router.register(r'books', BookViewSet)

urlpatterns = [
    path('api/', include(router.urls)),
]

That is it guys, check out the results using CURL [if you're on mac or linux, or you can download git bash on windows] => Check the video for better demonstration.

Conclusion
With the Django REST Framework, building APIs is a awesome (as i live always to say :) ).
I hope this guide gives you a strong foundation to get started. Got questions? Drop them in the comments!

𝕏: https:www.twitter.com/bekbrace

IG: https://www.instagram.com/bek_brace

...

🔧 Master API Development with Django REST Framework – Learn Django REST Framework for Free!


📈 52.77 Punkte
🔧 Programmierung

🔧 RESTful API in Django [Django Rest Framework]


📈 52.55 Punkte
🔧 Programmierung

🔧 Building a REST API with Django REST Framework: A Beginners Guide


📈 38.2 Punkte
🔧 Programmierung

🔧 Building REST API Endpoints with Django REST Framework: A Step-by-Step Guide


📈 38.2 Punkte
🔧 Programmierung

🔧 #Rest-Assured: A Powerful Framework for RESTful API Testing


📈 36.15 Punkte
🔧 Programmierung

🔧 Crafting Modern Web APIs with Django and Django REST Framework: A Comprehensive Guide


📈 32.03 Punkte
🔧 Programmierung

🔧 Django vs Django REST Framework: ¿Cuál es la diferencia? 🤔


📈 32.03 Punkte
🔧 Programmierung

🔧 Login with OTP Authentication in Django and Django REST Framework


📈 32.03 Punkte
🔧 Programmierung

🔧 Build a Multivendor E-commerce Website using Django, React & Django Rest Framework


📈 32.03 Punkte
🔧 Programmierung

🔧 Understanding REST API and RESTful: A Simple Guide with Everyday Examples


📈 29.77 Punkte
🔧 Programmierung

🔧 The 6 API Architecture Styles: REST, RESTful, GraphQL, SOAP, gRPC, WebSockets, and MQTT


📈 29.77 Punkte
🔧 Programmierung

🔧 Why your REST API might not be as RESTful as you think


📈 29.77 Punkte
🔧 Programmierung

🔧 Why your REST API might not be as RESTful as you think


📈 29.77 Punkte
🔧 Programmierung

🔧 Mastering REST API: An In-Depth Guide to Building and Utilizing RESTful Web Services


📈 29.77 Punkte
🔧 Programmierung

🔧 DevLog 20250519: AWS API Gateway Note - HTTP API vs Rest API for API Type


📈 29.65 Punkte
🔧 Programmierung

🔧 Master Django REST Framework: Build a Student Management System API with Function-Based Views


📈 28.94 Punkte
🔧 Programmierung

🔧 Standardizing API Responses in Django REST Framework: A Step-by-Step Guide


📈 28.94 Punkte
🔧 Programmierung

🔧 🚀 Django REST Framework: Installation & API Documentation Setup


📈 28.94 Punkte
🔧 Programmierung

🔧 How to Build a Task Manager API with Django REST Framework: Part 3 - Authentication and Permission


📈 28.94 Punkte
🔧 Programmierung

🔧 How to Build a Task Manager API with Django REST Framework: Part 2


📈 28.94 Punkte
🔧 Programmierung

🔧 How to Build a Task Manager API with Django REST Framework (Step-by-Step Guide)


📈 28.94 Punkte
🔧 Programmierung

🔧 About A powerful Speech-to-Text API built with Django REST Framework and AssemblyAI


📈 28.94 Punkte
🔧 Programmierung

🔧 The Best Way to Implement API Versioning in Django REST Framework (Agile) 💎


📈 28.94 Punkte
🔧 Programmierung

🔧 Build a Blog API With JWT Authentication Using Django Rest Framework


📈 28.94 Punkte
🔧 Programmierung

🔧 Build a Blog API With JWT Authentication Using Django Rest Framework


📈 28.94 Punkte
🔧 Programmierung

🕵️ CVE-2018-25045 | Django REST Framework up to 3.9.0 DRF Browsable API cross site scripting


📈 28.94 Punkte
🕵️ Sicherheitslücken

🕵️ Django REST Framework up to 3.11.1 API Viewer input validation


📈 28.94 Punkte
🕵️ Sicherheitslücken

🕵️ WP Live Chat Support up to 8.0.32 on WordPress REST API REST API Call privilege escalation


📈 28.72 Punkte
🕵️ Sicherheitslücken