Backend Todo App 06 Authentication and Permissions part 4 | Django Rest framework | API Development
Автор: LIFOC
Загружено: 2025-12-28
Просмотров: 18
This video covers:
===============
TODO App Backend 6:
Token Authentication in djangorestframework
===========================================
1 - Make model changes
-------------------------
2 - Add rest_framework.authtoken to INSTALLED_APPS in settings.py
-------------------------
'rest_framework.authtoken',
3 - Migrate changes
-------------------------
4 - Simple User Register
-------------------------
class UserRegisterAPIView(ListAPIView):
def post(self, request):
username = self.request.data.get('username')
password = self.request.data.get('password')
user_obj = User.objects.create_user(username= username, password= password)
user_obj.save()
return Response(
{
'message': 'User created successfully'
}
)
5 - Simple User Login
----------------------
class LoginAPIView(ListAPIView):
def post(self, request):
try:
username = self.request.data.get('username')
password = self.request.data.get('password')
user_obj = User.objects.filter(username=username).first()
token_obj = user_obj.check_password(password)
except User.DoesNotExist:
return Response(
{
'message': 'Invalid credentials'
}
)
except Exception as e:
return Response(
{
'message': 'Something went wrong',
'error': str(e)
}
)
6 - Simple User Logout
----------------------
class LogoutAPIView(ListAPIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]
def post(self, request):
try:
user = request.user
token, created = Token.objects.get_or_create(user=user)
token.delete()
return Response(
{
'message': 'Logout successful'
}
)
except User.DoesNotExist:
return Response(
{
'message': 'Invalid credentials'
}
)
except Exception as e:
return Response(
{
'message': 'Something went wrong',
'error': str(e)
}
)
Imports
from django.contrib.auth.hashers import check_password, make_password
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.authentication import TokenAuthentication
from django.contrib.auth.models import User
from rest_framework.authtoken.models import Token
For Enquiries:
===========
Instagram: / solutionizeit
Gmail: solutionizeit.x@gmail.com
Services offered:
==============
Business Software Solutions
Web Application Development
Mobile Application development
Personal Training
Machine Learning
OpenCV
AI Solutions
College Assignments or Projects
Prototype Building
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: