Django restframework API Blog #3
Hammaga salom.
Bugungi maqolamiz(video ham bor)da foydalanuvchilarni ro’yxatdan o’tishini va saytga kirishini o’rganamiz.
django-restframeworkda login va parol sal boshqacharoq ishlaydi. Login qilganda foydalanuvchiga tegishli token aniqlanadi va qaytariladi, agar u bo’lmasa yangi yaratilib foydalanuvchiga taqdim etiladi.
Token nega kerak?
Token foydalanuvchilarni aniqlab olish uchun ishlatilinadi. Restframeworkda token bo’lmasa, so’rovni qaysi foydalanuvchi yuborayotganini aniqlab bo’lmaydi. Shuning uchun token ishlatiladi.
Demak, nazariy qismdan, amaliy qismga o’tamiz.
Birinchi navbatda settings.py ga kirib, rest_framework.authtoken ni o’rnatib olamiz:
settings.py
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app', 'rest_framework', 'rest_framework.authtoken' #authtoken modelsini ustanovka qilamiza ]
endi
views.py ga kiramiz:
from rest_framework.views import APIView #kelayotgan so’rovni get yoki postligini aniqlab beradi
from rest_framework.response import Response #json ko’rinishida javob qaytaradi
from rest_framework.parsers import JSONParser #jsoni ob’yektga o’tkazib beradi va request.data ni ichiga joylashtiradi
from django.contrib.auth.models import User #djangoni o’zini tayyor foydalanuvchi modelini chaqirib olamiz
from rest_framework.authtoken.models import Token #rest_frameworkning token modelini chaqirib olamiz
from django.contrib.auth import authenticate #va djangoni foydalanuvchi login parolini tekshiradigan funksiya
views.py
class SignUp(APIView): def post(self,request): parser_classes = JSONParser username = request.data['username'] email = request.data['email'] first_name = request.data['first_name'] last_name = request.data['last_name'] password = request.data['password'] user_check = User.objects.filter(username=username).exists() if user_check: return Response({"code":"Bunday foydalanuvchioldin ro'yxatdan o'tgan"}) else: user = User.objects.create_user(username,email,password) user.first_name = first_name user.last_name = last_name user.save() token,create = Token.objects.get_or_create(user=user) return Response({"token":str(token)}) class SigNin(APIView): def post(self,request): parser_classes = JSONParser #kelayotgan jsoni obyektga o'tkazib beradi username = request.data['username'] password = request.data['password'] if username is None or password is None: return Response({"code":"username yoki passwordni to'ldirmagansiz"}) user = authenticate(username=username,password=password) if not user: return Response({"code":"login yoki parol noto'g'ri qayta urinib ko'ring"}) token,create = Token.objects.get_or_create(user=user) return Response({"token":str(token)})
urls.py
from django.urls import path from .views import CategoryList,CreateCategory,CategoryUpdate,CategoryDelete,SignUp,SigNin #botga urlga zapros keganda http://127.0.0.1:8000/api/list ga keganda categorylist class iwlidi urlpatterns = [ path('list',CategoryList.as_view()), path('create',CreateCategory.as_view()), path('update/',CategoryUpdate.as_view()) ,# ',CategoryDelete.as_view()), path('login',SigNin.as_view()), path('signup',SignUp.as_view()) ] #mana o'chirib ham bo'ldi endi shuni postmandan tekshirib ko'ramiz #BUGUNGI DARS TUGADI MAYLI HAMMAGA OMAD))
Ana endi
postman orqali tekshirib ko’rsak ham bo’ladi
Mover
YouTube
Ushbu dars kodini github’dan yuklab olishingiz mumkin.
Bugungi dars tugadi. O’ylaymanki, sizlarga dars yoqdi. Savolaringiz yoki qo’shimcha fikringiz bo’lsa, izohda qoldiring.
API
Django restframework API Blog #3