Civil Ximp; amp; Structural Engineering
Tworzenie Apis odpoczynkowych z Python i Django Framework
Table of Contents
Creating Restful API is essential for enabling communication between different the accordare systems. Python, combinad with Django Rest Framework (DRF), provides a powerful andd flexible ble way tu develop these API efficiently. This article coves thee basic steps to create a RESTful API using Python and DRF.
Setting Up the Environment
Początkowo był to projekt Python and Django. Use pip to install Django Rest Framework:
Xi1; Xi1; FLT: 0 Xi3; Xi3; Commands: Xi1; Xi1; FLT: 1 Xi3; Xi3;
quent; quentin; bash pip install django pip install djangorestframework quentiquent;
Stworzenie nowego projektu Django i app:
message; bash django-adnoun startproject myproject cd myproject python management; py startapp api message;
Definiing thee Data Model
In thee app directory, define a model in indic1; Ig1; FLT: 0 Iglo3; Iglome3;
Xi1; Xi1; FLT: 0 Xi3; Xi3; Example: Xi1; Xi1; FLT: 1 Xi3; Xi3;
Methinquet-; python from django.db import models class Item (models.Model): name = models.CharField (max _ length = 100) description = models.TexxtField () created _ at = models.DateTimeField (auto _ now _ add = True) contribution;
Serializers
Serializars convert model intances into JSON format. Create a present 1; British 1x1; FLT: 1 presentation 3x3; File in thee app directory:
Xi1; Xi1; FLT: 0 Xi3; Xi3; Example: Xi1; Xi1; FLT: 1 Xi3; Xi3;
message; phython frem rest _ framework import serializars frem .models import Item class ItemSerializar (serializars.ModelSerializar): class Meta: model = Item fields = containment; _ _ all _ _ containment; containment quote;
Creating Views andd URL
Definiować przeglądy API in 'endis1;' endis1; 'FLT: 2' endis3; 'endis3;
Xi1; Xi1; FLT: 0 Xi3; Xi3; Example: Xi1; Xi1; FLT: 1 Xi3; Xi3;
Methinquet-; python frem rest _ framework import generals frem .models import Item frem .serializars import ItemSerializar class ItemDetail (generals.ListCreateAPIView): queryset = Item.objects.all () serializar _ class = ItemSerializair class (generals.RetrieveUpdateDestroyAPIView): queryset = Item.objects.all () serializar _ class = ItemSerializair conclutext;
Konfiguracja URL in Xi1; Xi1; FLT: 3 Xi3; Xi3;:
Xi1; Xi1; FLT: 0 Xi3; Xi3; Example: Xi1; Xi1; FLT: 1 Xi3; Xi3;
message; indicated; python from django.urls import path from .views import ItemLiszt, ItemDetail urlPatterns = entical; path (enticates; items / enticate;, ItemList.as _ view (), name = enticate; item- ligt enticate;), path (enticates; items / / / enticate;, Itemme.as _ view (), name = enticates; item- detail enticame;), enticame;
Running thee API
Apely migrations andd run the development server:
Xi1; Xi1; FLT: 0 Xi3; Xi3; Commands: Xi1; Xi1; FLT: 1 Xi3; Xi3;
memoriał; memoriał; memoriał; memoriał; memoriał; memoriał; memoriał; memoriał; memoriał; memoriał; memoriał; memoriał; memoriał; memoriał;
Access thee API at indic1; EDI1; FLT: 4 EDI3; EDI3; to view, create, update, or delete items.