Django REST Framework/DRF 일반
[DRF] FBV, APIView의 예시
1. FBV 정리 및 예시
# urls.py
from django.urls import path, include
from . import views
urlpatterns = [
# FBV
path('cbv/post/', views.post_list),
path('cbv/post/<int:pk>/',views.post_detail),
]
# views.py
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Post
from .serializers import PostSerializer
from rest_framework.decorators import api_view
@api_view(['GET','POST'])
def post_list(request):
if request.method == 'GET':
qs = Post.objects.all()
serializer = PostSerializer(qs, many=True)
return Response(serializer.data)
else:
serializer = PostSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
@api_view(['GET','PUT','DELETE'])
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == 'GET':
serializer = PostSerializer(post)
return Response(serializer.data)
elif request.method == 'PUT':
serializer = PostSerializer(post, data=reqeust.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
post.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
2. APIView 정리 및 예시
1) APIView
from django.urls import path, include
from . import views
urlpatterns = [
# FBV
path('post/', views.PostListAPIView.as_view()),
path('post/<int:pk>/',views.PostDetailAPIView.as_view()),
]
# views.py
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Post
from .serializers import PostSerializer
# 포스팅 목록 및 새 포스팅 작성
class PostListAPIView(APIView):
def get(self, request):
serializer = PostSerializer(Post.objects.all(), many=True)
def post(self, request):
serializer = PostSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
from django.shortcuts import get_object_or_404
# 포스팅 내용, 수정, 삭제
class PostDetailAPIView(APIView):
def get_object(self, pk):
return get_object_or_404(Post, pk=pk)
def get(self, request, pk, format=None):
post = self.get_object(pk)
serializer = PostSerializer(post)
return Response(serializer.data)
def put(self, request, pk):
post = self.get_object(pk)
serializer = PostSerializer(post, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk):
post = self.get_object(pk)
post.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
- reference :
https://velog.io/@jcinsh/DRF-function-based-view
https://velog.io/@yvvyoon/django-rest-framework-1
'Django REST Framework > DRF 일반' 카테고리의 다른 글
[DRF] ViewSet, GenericViewSet, ModelViewSet 페이지 네이션 사용 방법 (1) | 2024.01.29 |
---|---|
[DRF] PostgreSQL에서 order_by로 한글 정렬 하기 (1) | 2024.01.26 |
[DRF] Request, Response의 간단한 정리 (0) | 2024.01.25 |
[DRF] TestCase로 Django-Rest-Framework를 위한 테스트 코드 만들기 (0) | 2024.01.23 |
[DRF] 공식 문서 - Caching 정리 (1) | 2024.01.22 |
댓글