Django restframework API Blog #1

Hammaga salom.

Bugungi video kursda django restframework orqali api yaratamiz(video maqolaning eng quyi qismida).

API o'zi nimaligi haqida bu yerda o'qib olishingiz mumkin.

Buyruqlar qatori(cmd) yoki terminalni ochamiz. Agar Sizda python o'rnatilmagan bo'lsa, shu maqolani ko'rib chiqishni tavsiya qilaman.

Avvalo django va django rest framework o'rnatib olamiz:

pip install django
pip install djangorestframework

Kerakli kutubxona va ish qurollari o'rnatib oldik. Endi startproject ochsak ham bo'ladi.

django-admin startproject blog
cd blog
python manage.py startapp app   #ana project ham ochib oldik endi blog ichiga krib settings.py fayli topib
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app',
    'rest_framework'
]

Yaratgan loyihamizni papkani ismini qo'shamiz va rest_framework kutubxonasini qo'shamiz.

Undan keyin app papkani ichidan model.py faylini topamiz. So'ng, category uchun model yaratamiz:

from django.db import models
# Create your models here.
#category uchun model yaratamiz
class Category(models.Model):
    name = models.CharField(max_length=255)
    def __str__(self):
        return self.name   #admin qismida name ko'rsatib turadi

model ham yaratib oldik, endi terminalni ochib

python manage.py makemigrations
python manage.py migrate       #modelimiz asosida ma'lumotlar omborida jadval yaratamiz.

Ana endi app papkani ichida serilazers.py va urls.py nomli fayllarni ochamiz:


serialzers.py faylini ichiga kirib, yasagan modelimizni chaqiramiz:

from rest_framework.serializers import ModelSerializer
from app.models import Category
class CategoryListAPIView(ModelSerializer):
    class Meta:
        model = Category
        fields = [
            'name' #name bu categoryda yaratgan qatorimiz
        ]

views.py faylga o'tamiz va u yerdan hozir yasagan serilazers.py ichidagi klassimizni chaqiramiz:

from django.shortcuts import render
from .serilazers import CategoryListAPIView
from rest_framework.generics import ListAPIView
from app.models import Category
# Create your views here.
class CategoryList(ListAPIView):
    queryset = Category.objects.all()
    serializer_class = CategoryListAPIView

Endi urls.py ga kirb havolani views.py'da yasagan klassimizga to'g'rilab qo'yamiz:

from django.urls import path
from .views import CategoryList
#bu urlga so'rov kelganda http://127.0.0.1:8000/api/list categorylist klass ishlaydi
urlpatterns = [
    path('list',CategoryList.as_view())
]

Keyingi qadamda blog papkani ichiga kirib, urls.py faylini ochib, havolani app da yasagan urls.py ga to'g'rilab qo'yamiz:

from django.contrib import admin
from django.urls import path,include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/',include('app.urls'))
]

Ana bo'ldi, endi terminalni ochib

python manage.py createsuperuser       #admin paneliga kirish uchun o'zimiz uchun foydalanuvchi yaratamiz


python manage.py runserver

Endi brauzer orqali http://127.0.0.1:8000/admin/ manziliga kiramiz:

Hozirgina yaratgan foydalanuvchi login va parolini teramiz va category'lar qo'shib chiqamiz:

Mana categoriy'lar ham qo'shib oldik. Endi api formatda ko'ramiz. Buning uchun http:127.0.0.1:8000/api/list manziliga kiramiz. Bu yerda admin panel orqali category'larga qo'shgan barcha ma'lumotlarimiz ko'rinadi:


Mover:


YouTube:

Ushbu dars kodini github'dan yuklab olishingiz mumkin.

Dars o'z nihoyasiga yetdi! O'ylaymanki, hammasi Siz uchun tushunarli bo'ldi. Django bo'yicha keyingi darslarda nimalarni ko'rsatish kerakligi bo'yicha takliflar yoki maqola yuzasidan savollar bo'lsa, ushbu maqolada izoh sifatida qoldirishingiz mumkin!


begyypy

Muallif haqida

Sadullayev Bekhzod Boxodirvich Python Django Backend Developer


Blogdagi so‘nggi maqolalar:


Birinchi bo‘ling!

Iltimos, fikr bildirish uchun saytga kiring yoki ro‘yxatdan o‘ting!